簡體   English   中英

r中多元回歸中預測函數的錯誤

[英]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.namenewdata$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM