Error: missing values and NaN's not allowed if 'na.rm' is FALSE

时间秒杀一切 提交于 2019-12-10 19:18:26

问题


Trying out multiple models chapter of #r4ds and ran into an error message at the end:

Error: missing values and NaN's not allowed if 'na.rm' is FALSE In addition: Warning message: In ns(as.numeric(Month), 4) : NAs introduced by coercion

with

ADA_model<- function(ADA_mutiple_model){
   lm(ADA ~ ns(as.numeric(Month), 4), data=ADA_mutiple_model)
}

ADA_mutiple_model <- ADA_mutiple_model %>% 
     mutate(model=map(data,ADA_model)) 

as the code I used that creates the error.

See mod3 below to see what the function looks like


回答1:


Your problem has nothing to do with the use of lm, but inside splines::ns when generating B-spline basis for natural cubic splines. Very likely your Month is a character variable, and you can not use as.numeric for coercing.


I just checked your attached figure. The x-axis in the plots verifies what I guessed. You need to use 1:12 for Month, not "JAN", "FEB", etc.



来源:https://stackoverflow.com/questions/38961475/error-missing-values-and-nans-not-allowed-if-na-rm-is-false

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