[R] re gression with replication

Ben Bolker bolker at ufl.edu
Tue Jul 14 00:00:34 CEST 2009




AgusSusanto wrote:
> 
> Dear all,
> I would like to fit a linear regression with replication (on each year,
> observation is replicated, e.g 4 times). The independent variable ranges
> for instance 1-5 year, so I expect to have a linear fit of 5 points.
> For that purpose I do these (with dummy variables x and y):
> 
> x<-rep(seq(1:5),4)
> y<-rnorm(20)
> linreg<-lm(y~x)
> fitted.values(linreg)  # why produce 20 points of estimate?
> predict(linreg)        # why produce 20 points of estimate?
> 
> Please somebody explain:
> 1. why both fitted.values and predict functions produced 20 points of
> estimate, NOT 5 points.
> 
>   Because R doesn't know your regression is replicated. It just knows you
> gave it
> 20 pairs of predictors and responses.
> 
>    Perhaps you want
> 
> predict(linreg,newdata=list(x=unique(x)))   ?
> 
> 2. is "lm(y~x)" correct to solve this regression case, or there's a
> correct procedure.
> 
> 

If you expected the samples within year to be independent of each other
(which seems a little dicey) then this would be correct; otherwise the
coefficients
will be correct but the residual df etc. will be wrong.  You could try
something like

ymeans  <- tapply(y,x,mean)
xvals <- unique(x)
lm(ymeans ~ xvals)

or a more complicated approach (probably unnecessary here):

library(nlme)

lme(y~x,random=~1|year)

[I think]


-- 
View this message in context: http://www.nabble.com/regression-with-replication-tp24468326p24470213.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list