On running a gam model using the mgcv package, I encountered a strange error message which I am unable to understand:
“Error in model.frame.default(formula = death ~ pm10 + Lag(resid1, 1) + : variable lengths differ (found for 'Lag(resid1, 1)')”.
The number of observations used in model1 is exactly the same as the length of the deviance residual, thus I think this error is not related to difference in data size or length.
I found a fairly related error message on the web here, but that post did not receive an adequate answer, so it is not helpful to my problem.
Reproducible example and data follows:
library(quantmod)
library(mgcv)
require(dlnm)
df <- chicagoNMMAPS
df1 <- df[,c("date","dow","death","temp","pm10")]
df1$trend<-seq(dim(df1)[1]) ### Create a time trend
Run the model
model1<-gam(death ~ pm10 + s(trend,k=14*7)+ s(temp,k=5),
data=df1, na.action=na.omit, family=poisson)
Obtain deviance residuals
resid1 <- residuals(model1,type="deviance")
Add a one day lagged deviance to model 1
model1_1 <- update(model1,.~.+ Lag(resid1,1), na.action=na.omit)
model1_2<-gam(death ~ pm10 + s(trend,k=14*7)+ s(temp,k=5) + Lag(resid1,1), data=df1,
na.action=na.omit, family=poisson)
Both of these models produced the same error message.
na.omit. Perhaps the differing lengths are due to an observation with an NA value being dropped. - joranna.omit. Note thatdfhas 5114 rows and the length ofresid1is only 4863. NA values are indeed being dropped. Try dropping the NA values first. Then your residual vector will match your original data frame. - joran