[R] Elegant way to express residual calculation in R?

2006-02-27 Thread John McHenry
Hi All, I am illustrating a simple, two-way ANOVA using the following data and I'm having difficulty in expressing the predicted values succinctly in R. X- data.frame(read.table(textConnection( Machine.1Machine.2Machine.3 53 61 51

Re: [R] Elegant way to express residual calculation in R?

2006-02-27 Thread Liaw, Andy
Not sure if the following would qualify as elegant... It's easier to work with a matrix instead of data frame: x.mat - data.matrix(X) xmean - mean(x.mat) operator.eff - ave(x.mat, row(x.mat)) - xmean machine.eff - ave(x.mat, col(x.mat)) - xmean (predicted.x - xmean + operator.eff + machine.eff)

Re: [R] Elegant way to express residual calculation in R?

2006-02-27 Thread Gabor Grothendieck
Try this: Xm - as.matrix(X) X.lm - lm(c(Xm) ~ factor(col(Xm)) + factor(row(Xm))) sum(resid(X.lm)^2) or if the idea was to do it without using lm try replacing your calculation of X.predicted with this: X.predicted - outer(operator.adjustment, machine.adjustment, +) + mean(mean(X)) On

Re: [R] Elegant way to express residual calculation in R?

2006-02-27 Thread Gabor Grothendieck
Here is one further idea: Xm - as.matrix(X) Xm. - scale(Xm, scale = FALSE) Xm.. - t(scale(t(tmp), scale = FALSE)) sum(Xm..^2) On 2/27/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Try this: Xm - as.matrix(X) X.lm - lm(c(Xm) ~ factor(col(Xm)) + factor(row(Xm))) sum(resid(X.lm)^2) or if