Error in model.frame.default: variable lengths differ

后端 未结 3 1324
醉梦人生
醉梦人生 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:51

    Its simple, just make sure the data type in your columns are the same. For e.g. I faced the same error, that and an another error:

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

    So, I went back to my excel file or csv file, set a filter on the variable throwing me an error and checked if the distinct datatypes are the same. And... Oh! it had numbers and strings, so I converted numbers to string and it worked just fine for me.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-01 13:03

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

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

    0 讨论(0)
提交回复
热议问题