From: <Bill.Venables_at_csiro.au>

Date: Fri, 20 May 2011 13:54:43 +1000

R-help_at_r-project.org 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.

R-help_at_r-project.org 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. Received on Fri 20 May 2011 - 03:57:51 GMT

Date: Fri, 20 May 2011 13:54:43 +1000

That only applies if you have the same factors a and b each time. If this is the case you can do things in a much more slick way.

u <- matrix(rnorm(5000), nrow = 10) ## NB, nrow
AB <- expand.grid(a = letters[1:2], b = letters[1:5])
M <- lm(u ~ a+b, AB)

rmsq <- colSums(resid(M)^2)/M$df.resid

and Bob's your uncle.

If you really want to do it quickly you would bypass lm() altogether and use something like ls.fit or, at an even lower level, qr() and qr.resid(). There are umpteen ways of fitting linear models.

Bill Venables.

-----Original Message-----

From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Dennis Murphy
Sent: Friday, 20 May 2011 1:38 PM

To: Cheryl Johnson

Cc: r-help_at_r-project.org

Subject: Re: [R] extraction of mean square value from ANOVA

Hi:

It's easier to use an apply family function than a loop for this type of problem, as illustrated below:

# Generate 30 random samples of size 10 from a standard # normal distribution and put them into a matrix

u <- matrix(rnorm(300), ncol = 10) a <- factor(rep(1:5, each = 2)) b <- factor(rep(1:2, 5))

# Side note: It's not a good idea to name an object 'c' because a commonly used function in the base package has that name already, as in c(1, 3, 5)...

# A function to fit the model and to compute the MSE # deviance() returns the residual sum of squares in a linear model meansq <- function(y) {

m <- lm(y ~ a + b)

deviance(m)/df.residual(m)

}

# Apply the function to each row of u; the result is a vector of MSEs
msevec <- apply(u, 1, meansq)

msevec

Note 2: The function works if a and b are in the same environment as the meansq() function. If you do all of this in the console, there should be no problem. If you decide to put all of this into a function, then you need to be more careful.

**HTH,
**

Dennis

On Thu, May 19, 2011 at 6:46 PM, Cheryl Johnson
<johnson.cheryl625_at_gmail.com> wrote:

> Hello,

*>
**> I am randomly generating values and then using an ANOVA table to find the
**> mean square value. I would like to form a loop that extracts the mean square
**> value from ANOVA in each iteration. Below is an example of what I am doing.
**>
**> a<-rnorm(10)
**> b<-factor(c(1,1,2,2,3,3,4,4,5,5))
**> c<-factor(c(1,2,1,2,1,2,1,2,1,2))
**>
**> mylm<-lm(a~b+c)
**> anova(mylm)
**>
**> Since I would like to use a loop to generate this several times it would be
**> helpful to know how to extract the mean square value from ANOVA.
**>
**> Thanks
**>
**> [[alternative HTML version deleted]]
**>
**> ______________________________________________
**> R-help_at_r-project.org 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.
**>
*

R-help_at_r-project.org 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.

R-help_at_r-project.org 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. Received on Fri 20 May 2011 - 03:57:51 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Fri 20 May 2011 - 04:20:10 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*