cv.glm variable lengths differ

半世苍凉 提交于 2019-12-02 02:46:25

问题


I am trying to cv.glm on a linear model however each time I do I get the error

Error in model.frame.default(formula = lindata$Y ~ 0 + lindata$HomeAdv +  : 
variable lengths differ (found for 'air-force-falcons')

air-force-falcons is the first variable in the dataset lindata. When I run glm I get no errors. All the variables are in a single dataset and there are no missing values.

> linearmod5<- glm(lindata$Y ~ 0 + lindata$HomeAdv + ., data=lindata, na.action="na.exclude")
> set.seed(1)
> cv.err.lin=cv.glm(lindata,linearmod5,K=10)
Error in model.frame.default(formula = lindata$Y ~ 0 + lindata$HomeAdv +  : 
variable lengths differ (found for 'air-force-falcons')

I do not know what is driving this error or the solution. Any ideas? Thank you!


回答1:


What is causing this error is a mistake in the way you specify the formula

This will produce the error:

mod <- glm(mtcars$cyl ~ mtcars$mpg + .,
            data = mtcars, na.action = "na.exclude")

cv.glm(mtcars, mod, K=11) #nrow(mtcars) is a multiple of 11

This not:

mod <- glm(cyl ~ ., data = mtcars)

cv.glm(mtcars, mod, K=11)

neither this:

mod <- glm(cyl ~ + mpg + disp, data = mtcars)

cv.glm(mtcars, mod, K=11)

What happens is that you specify the variable in like mtcars$cyl this variable have a number of rows equal to that of the original dataset. When you use cv.glm you partition the data frame in K parts, but when you refit the model on the resampled data it evaluates the variable specified in the form data.frame$var with the original (non partitioned) length, the others (that specified by .) with the partitioned length.

So you have to use relative variable in the formula (without $).

Other advices on formula:

avoid using a mix of specified variables and . you double variables. The dot is for all vars in the df except those on the left of tilde.

Why do you add a zero? if it is in the attempt to remove the intercept use -1 instead. However, this is a bad practice in my opinion



来源:https://stackoverflow.com/questions/28350357/cv-glm-variable-lengths-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!