Error in model.frame.default: variable lengths differ

后端 未结 3 1343
醉梦人生
醉梦人生 2020-12-01 12:11

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(fo

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 12:55

    Joran suggested to first remove the NAs before running the model. Thus, I removed the NAs, run the model and obtained the residuals. When I updated model2 by inclusion of the lagged residuals, the error message did not appear again.

    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)
    

    Obtain residuals

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

    Update model2 by including the lag 1 residuals

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

提交回复
热议问题