Ryan T. Donnelly
Ryan T. Donnelly

Reputation: 153

Inputting data to a matrix and predicting the response variable in r

So I'm having a problem combining a vector with a matrix

 require(faraway)
 x<-lm(lpsa~lcavol+lweight+age+svi+lcp+gleason+pgg45,prostate)
 y<-model.matrix(x)

I have been given new data, that I need to predict lpsa with. So I was thinking that I could just add the data in using a vector and go about the regression analysis from there.

 z<-c(1.44692,3.62301,65,.30010,0,-.79851,7,15)
 rbind(y,z)

Not only does this give me 100 rows, but I'm not sure how to predict lpsa using this method. Can anybody give me advice?

Upvotes: 0

Views: 127

Answers (1)

etienne
etienne

Reputation: 3678

Try :

require(faraway)
x<-lm(lpsa~lcavol+lweight+age+svi+lcp+gleason+pgg45,prostate)
z<-c(1.44692,3.62301,65,.30010,0,-.79851,7,15)
z<-z[-length(z)]
names(z)<-names(x$coefficients)[-1]
z<-as.list(z)
predict(x,z)

       1 
2.036906 

Explanation : when you create x you then have to use predict to predict lpsa for new values of your variables. You create a list z with as many variables as there are in the model (except lpsa as you wish to "find" it). You then run the command and 2 is the predicted value of lpsa for the new varaibles. AS for the last value of z (ie 15) I don't know what it is.

unlist(z) # this shows that z is coherent as age is 65 (only value that makes sense for it)
  lcavol  lweight      age      svi      lcp  gleason    pgg45 
 1.44692  3.62301 65.00000  0.30010  0.00000 -0.79851  7.00000 

If you want to know the coefficients calcultated by the regression you can do :

 coefficients(x)
 (Intercept)       lcavol      lweight          age          svi          lcp      gleason        pgg45 
-0.130150643  0.577486444  0.576247172 -0.014687934  0.698386394 -0.100954503  0.055762175  0.004769619 

If you want to be sure that predict is correct, do :

unname(sum(unlist(z)*coefficients(x)[-1])+coefficients(x)[1])
[1] 2.036906 # same estimated value for z

Upvotes: 1

Related Questions