Error in lm.fit(x,y,offset = offset, singular.ok,…) 0 non-NA cases with boxcox formula

余生长醉 提交于 2020-03-18 12:32:10

问题


I am trying to run a boxcox transformation with the following code:

urban1 <- subset(ski,urban <= 4,na.rm=TRUE)
ski$gender <- as.numeric((as.character(ski$gender)),na.rm=TRUE)
urban1 <- as.numeric((as.character(urban1)))
x <- (ski$gender*urban1)
y <- ski$EPSI.
bc <- boxcox(y ~ x) 
(trans <- bc$x[which.max(bc$y)]) 
model3 <- lm(y ~ x) 
model3new <- lm(y^trans ~ x)
ski$EPSI. <- ski$EPSI. + 1

But I keep getting this error:

Error in lm.fit(x,y,offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: ... eval -> eval -> boxcar -> boxcar.formula -> lm -> lm.fit Execution halted

Thanks in advance!


回答1:


The error message

lm.fit(x,y,offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases

is generated by the lm(y ~ x) command when variables x or y (or both) have only NAs.
Here is an example:

n <- 10
x <- rnorm(n,1)
y <- rep(NA,n)
lm(y ~ x)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

In your code I suggest to test (just before your lm commands) if one of your variables has all NAs using:

all(is.na(x))
all(is.na(y))
all(is.na(y^trans))

In my example:

all(is.na(y))
[1] TRUE



回答2:


The error can be triggered by NA's in your data or a bad transformation

#From the mtcars dataset
mpg.reg3 <- lm(mpg ~ cylinders + displacement + horsepower + weight + acceleration + year + origin, data=Auto, na.action=na.exclude)

Notice the na.action= argument. Setting this to na.exclude will allow the lm function to ignore NA's in your data. Another option is na.omit which acts in a slightly different manner.

The other problem may be a bad transformation of your data- double check your interaction terms and manipulations.



来源:https://stackoverflow.com/questions/43677853/error-in-lm-fitx-y-offset-offset-singular-ok-0-non-na-cases-with-boxcox

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