Reputation: 2309
Basically, I am running nlme::lme
with a missing value. The model fits fine with na.action=na.omit
, but how come the name of the fitted/residuals/coef all seem to be shifted by a row?
## Generate data ---------------------
X1=gl(2,4)
X2=gl(2,2,8)
Y=rnorm(8)
dat=data.frame(Y=Y,X1=X1,X2=X2)
dat
## missing value -------------
mis.dat=dat
mis.dat[3,"Y"]=NA
mis.dat
> mis.dat
Y X1 X2
1 -0.06845332 1 1
2 0.89169085 1 1
3 NA 1 2
4 1.88997449 1 2
5 0.95912879 2 1
6 -0.64049400 2 1
7 -0.23354948 2 2
8 -0.66869350 2 2
## Fit model -----------------------
model=nlme::lme(Y~1,random=~1|X1/X2,data=mis.dat,na.action=na.omit)
summary(model)
## Notie the names -------------------
fitted(model)
> fitted(model)
1/1 1/1 2/1 2/1 2/2 2/2 <NA>
0.67179438 0.67179438 0.67179439 0.02855517 0.02855517 0.02855517 0.02855517
attr(,"label")
[1] "Fitted values"
#model$coef$random
#resid(model)
Notice how the names of the fitted value? Shouldn't there be a 1/2 in the 3rd position, and the names after that shift one position to the right, thus eliminating the NA?
Upvotes: 0
Views: 180
Reputation: 132706
You found a minor bug. Look at nlme:::fitted.lme
to see what happens:
The fitted values are extracted from the model using
object[["fitted"]]
fixed X1 X2
1 0.6014526 0.1686912 0.1686912
2 0.6014526 0.1686912 0.1686912
4 0.6014526 0.1686912 0.1686912
5 0.6014526 1.0342140 1.0342140
6 0.6014526 1.0342140 1.0342140
7 0.6014526 1.0342140 1.0342140
8 0.6014526 1.0342140 1.0342140
Note how there are 8 fitted values even though observation 3 was omitted from the fit due to the missing y
-value and shouldn't be there. The names are then created from
object[["groups"]]
X1 X2
1 1 1/1
2 1 1/1
3 1 1/2
4 2 2/1
5 2 2/1
6 2 2/2
7 2 2/2
Note how there are only 7 names. The NA gets introduced, when match
is used.
Ultimately the problem is in lme
, which should only return 7 fitted values. However, I don't have time to find out how this could be fixed. Feel free to report it.
Upvotes: 1