【问题标题】:error in predict function in multiple regression in rr中多元回归中预测函数的误差
【发布时间】:2017-10-26 05:39:48
【问题描述】:

我该如何解决这个错误

>attach(Depression_1)
> logistic<-lm(`any chronic illness in last year?`~`age in years at last birthday`+EDUCAT+`thousands of dollars per year`+`depressed is cesd >= 16`+`regular drinker?`)
> newdata<-data.frame(`age in years at last birthday`=34,EDUCAT=3,`thousands of dollars per year`=20,`depressed is cesd >= 16`=0,`regular drinker?`=1)
> predict(logistic,newdata)

警告信息:

'newdata' had 1 row but variables found have 294 rows

【问题讨论】:

标签: r linear-regression predict


【解决方案1】:

这是由于 newdata 和 Depression_1 之间的列名不同造成的问题。

错误在于创建数据框newdata的步骤,

newdata<-data.frame(`age in years at last birthday`=34,EDUCAT=3,`thousands of dollars per year`=20,`depressed is cesd >= 16`=0,`regular drinker?`=1)

> newdata
  age.in.years.at.last.birthday EDUCAT thousands.of.dollars.per.year depressed.is.cesd....16
1                            34      3                            20                       0
  regular.drinker.
1                1

注意如何将列名中的空格和? 替换为.,原因是这些不能满足数据框访问newdata$column.name 形式的列的要求。所以,要修复这个添加参数 check.names = FALSE 到 data.frame 函数作为

newdata <- data.frame(`age in years at last birthday`=34,EDUCAT=3,`thousands of dollars per year`=20,`depressed is cesd >= 16`=0,`regular drinker?`=1, check.names = FALSE)

> newdata
  age in years at last birthday EDUCAT thousands of dollars per year depressed is cesd >= 16
1                            34      3                            20                       0
  regular drinker?
1                1

【讨论】:

    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2018-04-27
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多