在R中为一行新输入数据运行predict.lm之后,我得到了许多预测

在R中为一行新输入数据运行predict.lm之后,我得到了许多预测,r,statistics,prediction,R,Statistics,Prediction,我使用包含83784行的ApacheData数据构建了一个线性回归模型: fit <-lm(tomorrow_apache~ as.factor(state_today) +as.numeric(daily_creat) + as.numeric(last1yr_min_hosp_icu_MDRD) +as.numeric(bun) +as.numeric(urin) +as.numeric(cat

我使用包含83784行的ApacheData数据构建了一个线性回归模型:

fit <-lm(tomorrow_apache~ as.factor(state_today)
         +as.numeric(daily_creat) 
         + as.numeric(last1yr_min_hosp_icu_MDRD)
         +as.numeric(bun)
         +as.numeric(urin)
         +as.numeric(category6)
         +as.numeric(category7)
         +as.numeric(other_fluid)
         + as.factor(daily)
         + as.factor(age)
         + as.numeric(apache3) 
         + as.factor(mv)
         + as.factor(icu_loc) 
         + as.factor(liver_tr_before_admit)  
         + as.numeric(min_GCS)
         + as.numeric(min_PH)  
         + as.numeric(previous_day_creat)  
         + as.numeric(previous_day_bun) ,ApacheData)
我期望一个值作为对这个新输入的预测,但我得到了很多预测!我不知道为什么会这样。我做错了什么


非常感谢您抽出时间

您还可以尝试R中的卓越效果软件包(
?effects
)。通过将等式右侧的输入设置为特定值,这对于绘制模型中的预测概率非常有用。我无法重现您在问题中给出的示例,但为了让您了解如何快速提取R中的预测概率,然后绘制它们(因为这对于理解它们的含义至关重要),这里有一个玩具示例,使用R中的内置数据集:

install.packages("effects") # installs the "effects" package in R
library(effects) # loads the "effects" package
data(Prestige) # loads in-built dataset
m <- lm(prestige ~ income + education + type, data=Prestige) 

# this last step creates predicted values of the outcome based on a range of values
# on the "income" variable and holding the other inputs constant at their mean values
eff <- effect("income", m, default.levels=10) 
plot(eff) # graphs the predicted probabilities
install.packages(“effects”)#在R中安装“effects”包
库(效果)#加载“效果”包
数据(Prestige)#加载内置数据集

m参见
?predict.lm
,没有名为
data
的参数。我们需要一个可复制的示例来帮助您。如果您在将数据发送到
lm
之前准备好数据,它会更干净。这里有一个页面可能会帮助您制作一个示例:尝试使用
newdata
而不是
data
。我使用了“newdata”而不是“data”,我得到了错误:model.frame.default中的错误(Terms,newdata,na.action=na.action,xlev=object$xlevels):factor as.factor(daily)has new level 2daily在我构建lm模型时使用的原始数据中等于2,我通过运行以下命令剪切原始数据:ApacheData$daily
install.packages("effects") # installs the "effects" package in R
library(effects) # loads the "effects" package
data(Prestige) # loads in-built dataset
m <- lm(prestige ~ income + education + type, data=Prestige) 

# this last step creates predicted values of the outcome based on a range of values
# on the "income" variable and holding the other inputs constant at their mean values
eff <- effect("income", m, default.levels=10) 
plot(eff) # graphs the predicted probabilities