model.frame.default(Terms,newdata,na.action=na.action,xlev=object$xlevels)中的错误:变量长度不同(为';因子(DAF)';)
我正在使用一个回归模型model.frame.default(Terms,newdata,na.action=na.action,xlev=object$xlevels)中的错误:变量长度不同(为';因子(DAF)';),r,R,我正在使用一个回归模型(MWTBest=428-7.69*糖尿病-72.1*心房纤颤-130*DAF),如果我在变量糖尿病和心房纤颤中替换1或0,我将尝试快速查看模型的值。当我使用prediction()时,我很难理解为什么会出现此错误: 我的所有变量“Diabetes”、“AtrialFib”和“DAF”都具有相同的长度,并且没有任何缺失值: > length(COPD$Diabetes) [1] 101 > length(DAF) [1] 101 > length(COPD
(MWTBest=428-7.69*糖尿病-72.1*心房纤颤-130*DAF)
,如果我在变量糖尿病
和心房纤颤
中替换1或0,我将尝试快速查看模型的值。当我使用prediction()
时,我很难理解为什么会出现此错误:
我的所有变量“Diabetes”、“AtrialFib”和“DAF”都具有相同的长度,并且没有任何缺失值:
> length(COPD$Diabetes)
[1] 101
> length(DAF)
[1] 101
> length(COPD$AtrialFib)
[1] 101
> sum(is.na(COPD$Diabetes))
[1] 0
> sum(is.na(COPD$DAF))
[1] 0
> sum(is.na(COPD$AtrialFib))
[1] 0
以下是有关我的数据集的更多信息:
> str(COPD)
'data.frame': 101 obs. of 24 variables:
$ X : int 1 2 3 4 5 6 7 8 9 10 ...
$ ID : int 58 57 62 145 136 84 93 27 114 152 ...
$ AGE : int 77 79 80 56 65 67 67 83 72 75 ...
$ PackHistory : num 60 50 11 60 68 26 50 90 50 6 ...
$ COPDSEVERITY: Factor w/ 4 levels "MILD","MODERATE",..: 3 2 2 4 3 2 3 3 2 3 ...
$ MWT1 : int 120 165 201 210 204 216 214 214 231 226 ...
$ MWT2 : int 120 176 180 210 210 180 237 237 237 240 ...
$ MWT1Best : int 120 176 201 210 210 216 237 237 237 240 ...
$ FEV1 : num 1.21 1.09 1.52 0.47 1.07 1.09 0.69 0.68 2.13 1.06 ...
$ FEV1PRED : num 36 56 68 14 42 50 35 32 63 46 ...
$ FVC : num 2.4 1.64 2.3 1.14 2.91 1.99 1.31 2.23 4.38 2.06 ...
$ FVCPRED : int 98 65 86 27 98 60 48 77 80 75 ...
$ CAT : int 25 12 22 28 32 29 29 22 25 31 ...
$ HAD : num 8 21 18 26 18 21 30 2 6 20 ...
$ SGRQ : num 69.5 44.2 44.1 62 75.6 ...
$ AGEquartiles: int 4 4 4 1 1 2 2 4 3 3 ...
$ copd : int 3 2 2 4 3 2 3 3 2 3 ...
$ gender : Factor w/ 2 levels "0","1": 2 1 1 2 2 1 1 2 2 1 ...
$ smoking : int 2 2 2 2 2 1 1 2 1 2 ...
$ Diabetes : int 1 1 1 0 0 1 1 1 1 0 ...
$ muscular : int 0 0 0 0 1 0 0 0 0 1 ...
$ hypertension: int 0 0 0 1 1 0 0 0 0 0 ...
$ AtrialFib : int 1 1 1 1 0 1 1 1 1 0 ...
$ IHD : int 0 1 0 0 0 0 0 0 0 0 ...
我通过将糖尿病和心房纤颤相乘来创建DAF,还有更多关于r123的信息
> DAF<-COPD$Diabetes*COPD$AtrialFib
> str(DAF)
int [1:101] 1 1 1 0 0 1 1 1 1 0 ...
> r123<-lm(MWT1Best~factor(Diabetes)+factor(AtrialFib)+factor(DAF), data=COPD)
> summary(r123)
Call:
lm(formula = MWT1Best ~ factor(Diabetes) + factor(AtrialFib) +
factor(DAF), data = COPD)
Residuals:
Min 1Q Median 3Q Max
-218.15 -51.88 18.70 51.85 270.86
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 428.14 10.39 41.200 < 2e-16 ***
factor(Diabetes)1 -7.69 28.02 -0.274 0.78436
factor(AtrialFib)1 -72.05 29.21 -2.467 0.01541 *
factor(DAF)1 -130.11 47.70 -2.727 0.00759 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 86.32 on 96 degrees of freedom
(1 observation deleted due to missingness)
Multiple R-squared: 0.3635, Adjusted R-squared: 0.3437
F-statistic: 18.28 on 3 and 96 DF, p-value: 1.841e-09
>DAF str(DAF)
int[1:101]10100110。。。
>r123摘要(r123)
电话:
lm(公式=MWT1最佳~因子(糖尿病)+因子(心房颤动)+
系数(DAF),数据=COPD)
残差:
最小1季度中值3季度最大值
-218.15 -51.88 18.70 51.85 270.86
系数:
估计标准误差t值Pr(>t)
(截距)428.14 10.39 41.200<2e-16***
因子(糖尿病)1-7.69 28.02-0.274 0.78436
系数(AtrialFib)1-72.05 29.21-2.467 0.01541*
系数(DAF)1-130.11 47.70-2.727 0.00759**
---
签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
剩余标准误差:96自由度时为86.32
(1项因缺失而删除的观察)
倍数R平方:0.3635,调整后的R平方:0.3437
F-统计量:3和96 DF上的18.28,p-值:1.841e-09
由于您的一个预测值是其他两个预测值的函数,因此出现这种情况,您对DAF预测值使用的预测
函数似乎不正确
因为我没有你的模型来测试,这更像是一个使用BaseR的预测函数的蛮力解决方案。在这里,我们将生成2个预测变量和派生变量的所有可能组合(本例中仅4个组合)
将+替换为*后,模型将考虑所有交互。我不确定哪个包包含
预测功能。但是basepredict()
函数使用的是数据帧而不是列表。谢谢你的指针。该函数来自同名的包prediction
,它应该通过predict
从模型对象中提取预测值并返回数据帧。我试图像你说的那样删除列表,但不幸地得到了一个类似的错误:>预测(r123,data=find_data(r123,parent.frame(COPD$Diabetes)),at=list(Diabetes=c(0,1)))model.frame.default中的错误(Terms,newdata,na.action=na.action,xlev=object$xlevels):变量长度不同(为“factor(DAF)”找到)
感谢您抽出时间回答。我使用了您的第一种方法,但不幸的是model.frame.default(Terms,newdata,na.action=na.action,xlev=object$xlevels)中又出现了一个错误:'data'必须是data.frame,而不是矩阵或数组。
函数predict
似乎不接受矩阵。第二种方法有效,我只是想知道是否有一种更简单的方法可以代替预测变量中的1和0,而不必手动计算?哦,实际上第二种方法非常有效!我基本上像你说的那样删除了DAF,效果很好:r123@inkyfingers,很高兴它现在起作用了。我修复了第一个选项的data.frame错误。请考虑接受答案来结束这个问题。第一个答案现在也起作用了!我不得不在谷歌上搜索如何接受答案,但我没有意识到这是可以做到的。我接受了,再次感谢!
> DAF<-COPD$Diabetes*COPD$AtrialFib
> str(DAF)
int [1:101] 1 1 1 0 0 1 1 1 1 0 ...
> r123<-lm(MWT1Best~factor(Diabetes)+factor(AtrialFib)+factor(DAF), data=COPD)
> summary(r123)
Call:
lm(formula = MWT1Best ~ factor(Diabetes) + factor(AtrialFib) +
factor(DAF), data = COPD)
Residuals:
Min 1Q Median 3Q Max
-218.15 -51.88 18.70 51.85 270.86
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 428.14 10.39 41.200 < 2e-16 ***
factor(Diabetes)1 -7.69 28.02 -0.274 0.78436
factor(AtrialFib)1 -72.05 29.21 -2.467 0.01541 *
factor(DAF)1 -130.11 47.70 -2.727 0.00759 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 86.32 on 96 degrees of freedom
(1 observation deleted due to missingness)
Multiple R-squared: 0.3635, Adjusted R-squared: 0.3437
F-statistic: 18.28 on 3 and 96 DF, p-value: 1.841e-09
#Devise the new test matrix
predictdf<-expand.grid(Diabetes=c(0,1), AtrialFib = c(0,1))
predictdf$DAF <- predictdf$Diabetes * predictdf$AtrialFib
#convert from integers to factors (to match the model)
predictdf<-apply(predictdf, 2, factor)
#preform the prediction
predict(r123, data.frame(predictdf))
lm(MWT1Best~factor(Diabetes)*factor(AtrialFib), data=COPD)