在构建泊松回归模型之后,我们可以使用R中的预测函数来构建预测。但是,我有一些问题,我无法解决我在R中设置的新数据。它给出了一个错误。
newdata <- data.frame(
patient = mean(myd$patient),
hypertensive = factor(1:4, levels = 1:4, labels = levels(myd$hypertensive)),
PCOS = factor(1:2, levels = 1:2, labels = levels(myd$PCOS)),
age = factor(1:3, levels = 1:3, labels = levels(myd$age)),
miscarriage = factor(1:2, levels=1:2, labels = levels(myd$miscarriage)))
data.frame出错(患者=平均值(myd $病人),高血压=因素(1:4,: 参数意味着不同的行数:1,4,2,3
答案 0 :(得分:0)
使用expand.grid
作为@Neal Fultz写道。你的变量在data.frame
的参数中长度为2-4,这要求它们都具有相同的长度。
我不知道为什么有人低估了我的答案但是我在这里扩展我的答案以使其清楚。问题是data.frame
的输入参数具有不同的长度
> # create data example
> myd <- data.frame(
+ # NOTICE: I assume that your patient covariate is continuous -- otherwise I
+ # do not get why you would take a mean
+ patient = 1:12,
+ # here are the factors
+ hypertensive = rep(letters[1:4], 3),
+ PCOS = rep(letters[1:2], 6),
+ age = rep(letters[1:3], 4),
+ miscarriage = rep(letters[1:2], 6)
+ )
> myd # show data
patient hypertensive PCOS age miscarriage
1 1 a a a a
2 2 b b b b
3 3 c a c a
4 4 d b a b
5 5 a a b a
6 6 b b c b
7 7 c a a a
8 8 d b b b
9 9 a a c a
10 10 b b a b
11 11 c a b a
12 12 d b c b
>
> # your issue is that the length you pass to data.frame differs
> length(factor(1:4, levels = 1:4, labels = levels(myd$hypertensive)))
[1] 4
> length(factor(1:2, levels = 1:2, labels = levels(myd$PCOS)))
[1] 2
>
> # however, you can use expand.grid
> newdata <- expand.grid(
+ patient = mean(myd$patient),
+ hypertensive = factor(1:4, levels = 1:4, labels = levels(myd$hypertensive)),
+ PCOS = factor(1:2, levels = 1:2, labels = levels(myd$PCOS)),
+ age = factor(1:3, levels = 1:3, labels = levels(myd$age)),
+ miscarriage = factor(1:2, levels=1:2, labels = levels(myd$miscarriage)))
>
> # it will have many examplse though
> nrow(newdata)
[1] 48