Create a function f which, given Species s, calculates the residual sum
of squares and then sapply it to all Species.  In your case Species is
the month and the levels of the Species correspond to the months 1:12

# calculate rss for Species s
f <- function(s) {
   iris.lm <- lm(Sepal.Width ~ Sepal.Length, iris, subset = Species == s)
   sum(resid(iris.lm)^2)
}

sapply(levels(iris$Species), f)

Please see the last line of every message to r-help and in particular
provide reproducible code in your postings.  That means either providing
a snippet of your data or using an analogous builtin dataset.


On 10/20/06, Dan Chan <[EMAIL PROTECTED]> wrote:
> Hi,
>
>
>
> I have a dataframe Wash2005, which has daily weather data.  I am doing a
> regression by month as follows:
>
> # FM10 Regression by Month
>
> # Plot 12 Month of OFM10, FFM10
>
> for(i in 1:12) {
>
> Temp <- subset (Wash2005, MM == i)
>
> assign( paste('Wash2005FM10', strtrim(month.name[i],3),
> sep=""),lm(Temp$FM10.1 ~ Temp$FM10))
>
> }
>
>
>
> Wash2005FM10Jan, Wash2005FM10Feb, etc holds the regression results for
> each month.
>
>
>
> Now, I want to sum up the residuals of all the 12 regression.  How can I
> do that in a loop?
>
> For (i in 1:12){
>
> Temp <- paste('sum(Wash2005FM10', strtrim(month.name[i],3),
> '$residual**2)',sep="")
>
> }
>
>
>
> Temp will give a string "sum(Wash2005FM10Jan$residual**2)".  How can I
> get R to evaluate this string?
>
>
>
> Thank you.
>
>
>
>
>
> Daniel Chan
>
> Meteorologist
>
> Georgia Forestry Commission
>
> P O Box 819
>
> Macon, GA
>
> 31202
>
> Tel: 478-751-3508
>
> Fax: 478-751-3465
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to