Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中predict.lm中的错误:因子为。因子(每日)具有新的级别2_R_Statistics_Prediction - Fatal编程技术网

R中predict.lm中的错误:因子为。因子(每日)具有新的级别2

R中predict.lm中的错误:因子为。因子(每日)具有新的级别2,r,statistics,prediction,R,Statistics,Prediction,我建立了一个线性回归模型,如下所示: ApacheData$daily <- cut(ApacheData$daily, breaks=c(-1, 0, 1, 2, 3, 9,3000)) ApacheData$age <- cut(ApacheData$age, breaks=c(0,44,65,150)) fit <-lm(tomorrow_apache~ as.factor(state_today) +as.numeric(daily_creat

我建立了一个线性回归模型,如下所示:

 ApacheData$daily <- cut(ApacheData$daily, breaks=c(-1, 0, 1, 2, 3, 9,3000))
 ApacheData$age <- cut(ApacheData$age, breaks=c(0,44,65,150))

 fit <-lm(tomorrow_apache~ as.factor(state_today)
         +as.numeric(daily_creat) 
         + as.factor(daily)
         + as.factor(age)
         +as.numeric(apache3) 
         + as.factor(mv)  
         + as.numeric(min_GCS), ApacheData)
我得到了一个错误: model.frame.default(术语、新数据、na.action=na.action、xlev=object$xlevels)中出错: 因子为。因子(每日)有新的级别2

我还尝试了data.frame()中的daily=as.factor(2),得到了相同的错误。有人能帮我吗


非常感谢您抽出时间

您的原始数据没有任何情况下
ApacheData$daily==2
lm
对象没有与其关联的系数,因此它会抛出一个错误。

在运行此操作之前:ApacheData$daily Run
cut(2,breaks=c(-1,0,1,2,3,93000))
查看它生成的标签。您需要对新数据运行相同的函数,以确保标签的一致性。当我运行cut(2,breaks=c(-1,0,1,2,3,93000))时,您还可以使用
cut(…,labels=)
为每个级别指定所需的标签。我得到了以下结果:[1](1,2)级别:(-1,0](0,1)(1,2)(2,3)(3,9](9,3e+03)是的,它已经自动为间隔生成了标签。新数据需要具有相同的标签,因此您应该通过相同的
cut()
函数传递
daily
。我在is.data.frame(data)中得到错误
错误:找不到对象“ApacheData”
。请使您的问题重复。
new <- data.frame(state_today=1, daily_creat=2.3, daily=2 , age=25, apache3=12, mv=1,     min_GCS=20)'
 predict(fit, new , se.fit=TRUE)