[英]error in predict function in multiple regression in r
我該如何解決這個錯誤
>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
由於 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
以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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.