简体   繁体   中英

error in predict function in multiple regression in r

how do I solve this error

>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)

Warning message:

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

This is a problem due to difference in column names between newdata and Depression_1.

Error lies in step of creating dataframe 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

Notice how spaces and ? are replaced by . in columnnames, the reason being these won't satisfy requirement of dataframe to access columns in form newdata$column.name . So, to fix this add argument check.names = FALSE to data.frame function as

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM