-1

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
Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83
  • 5
    Possible duplicate of [Getting Warning: " 'newdata' had 1 row but variables found have 32 rows" on predict.lm in R](https://stackoverflow.com/questions/27464893/getting-warning-newdata-had-1-row-but-variables-found-have-32-rows-on-pred) – Hardik Gupta Oct 26 '17 at 05:44
  • it's a warning, not an error – desertnaut Oct 26 '17 at 17:04

1 Answers1

0

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
parth
  • 1,571
  • 15
  • 24