Abc123 - 7 months ago 73

R Question

I am suppose to use the predict function to predict when

`fjbjor`

This is my code

`fit.lm <- lm(fjbjor~amagn, data=bjor)`

summary(fit.lm)

new.bjor<- data.frame(fjbjor=5.5)

predict(fit.lm,new.bjor)

and this comes out

`1 2 3 4 5 6 7 8 9 10 11`

5.981287 2.864521 9.988559 5.758661 4.645530 2.419269 4.645530 5.313409 6.871792 3.309773 4.200278

12 13 14 15 16

3.755026 5.981287 5.536035 1.974016 3.755026

Warning message: 'newdata' had 1 row but variables found have 16 rows

If anyone can see what is wrong I would be really thankful for the help.

Answer

Your model is `fjbjor ~ amagn`

, where `fjbjor`

is response and `amagn`

is covariate. Then your `newdata`

is `data.frame(fjbjor=5.5)`

.

`newdata`

should be used to provide covariates rather than response. `predict`

will only retain columns of covariates in `newdata`

. For your specified `newdata`

, this will be `NULL`

. As a result, `predict`

will use the internal model frame for prediction, which returns you fitted values.

The warning message is fairly clear. `predict`

determines the expected number of predictions from `nrow(newdata)`

, which is 1. But then what I described above happened so 16 fitted values are returned. Such mismatch produces the warning.

Looks like the model you really want is: `amagn ~ fjbjor`

.