I can not predict new observations with predict ()

0

Ok then I have the following information:

A<-c(.04,.10,.16,.28,.04,.10,.16,.28,.04,.10,.16,.28)
Gain<-c(680.2,721.4,750.4,789.4,672.2,709.2,731.2,778.2,668.4,715.6,732.0,794.0)

So I want to predict a new observations that is .20 using a simple linear regression model, then I do:

new.dat<-data.frame(porcentaje=.20)
predict(lm(Gain~A), newdata=new.dat, interval="confidence")

But all I get are all the predictions of the observations and an error message:

        fit      lwr      upr
1  680.4533 671.1511 689.7555
2  707.9181 701.1857 714.6505
3  735.3829 729.2931 741.4726
4  790.3124 779.3810 801.2437
5  680.4533 671.1511 689.7555
6  707.9181 701.1857 714.6505
7  735.3829 729.2931 741.4726
8  790.3124 779.3810 801.2437
9  680.4533 671.1511 689.7555
10 707.9181 701.1857 714.6505
11 735.3829 729.2931 741.4726
12 790.3124 779.3810 801.2437
Warning message:
'newdata' had 1 row but variables found have 12 rows

I do not understand what I'm doing wrong, please help!

    
asked by Jerry Jiménez 27.11.2017 в 01:55
source

1 answer

1

The problem you have is that you are not building correctly the data.frame test, you are doing:

new.dat<-data.frame(porcentaje=.20)

This data.frame does not correspond to the one you used to create the model, the correct thing according to your example would be to name the column porcentaje as you have named it in data.frame of training, for example:

new.dat<-data.frame(A=.20)
predict(lm(Gain~A), newdata=new.dat, interval="confidence")

       fit      lwr     upr
1 753.6927 746.6284 760.757
    
answered by 27.11.2017 в 15:00