Error in model.frame.default ...... variable length is different

When running the gam model using the mgcv package, I found a strange error message that I cannot understand. The error message was "Error in model.frame.default (formula = death ~ pm10 + Lag (residue1, 1) +: variable length is different (found for" Lag (residue1, 1) "). The number of observations used in model1 exactly the same as the length of the residual deviation, so I think this error is not related to the difference in the size or length of the data.

I found a pretty related error message on the website here , but this post did not receive an adequate response, so my problem does not help me

Reproducible example and data:

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 model

 model1<-gam(death ~ pm10 + s(trend,k=14*7)+ s(temp,k=5), data=df1, na.action=na.omit, family=poisson) 

Get rejection balances

 resid1 <- residuals(model1,type="deviance") 

Add one day backlog from 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 issued the same error message.

+13
r
source share
3 answers

Joran suggested removing NA first before running the model. So I deleted NA, ran the model and got the rest. When I updated model2 by including lagging residuals, the error message no longer appeared.

Remove NAs

 df2<-df1[complete.cases(df1),] 

Run the main model

 model2<-gam(death ~ pm10 + s(trend,k=14*7)+ s(temp,k=5), data=df2, family=poisson) 

Get leftovers

 resid2 <- residuals(model2,type="deviance") 

Update Model2 to include lag residues 1

 model2_1 <- update(model2,.~.+ Lag(resid2,1), na.action=na.omit) 
+12
source share

Simple, just make sure the data type in your columns is the same. E.g. I came across the same error, and with another error:

Error in contrasts<- ( *tmp* , value = contr.funs [1 + isOF [nn]]): contrasts can only be applied to factors with 2 or more levels

So, I went back to the excel file or csv file, set the filter for the variable, resetting me with an error and checked if the individual data types are the same. AND ABOUT! it had numbers and strings, so I converted the numbers to a string, and it worked fine for me.

+4
source share

Another thing that can cause this error is to create a model with the centering / scaling standardization function from the hand package - m <- standardize(lm(y ~ x, data = train))

If you then try predict(m) , you will get the same error as in this question.

+3
source share

All Articles