[R] Predicted value at a new level in lm

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Oct 17 23:16:23 CEST 2006


On Tue, 2006-10-17 at 12:34 -0700, Li Zhang wrote:
>     Y       X     Z 
>    42.0    7.0   33.0
>    33.0    4.0   41.0
>    75.0   16.0    7.0
>    28.0    3.0   49.0
>    91.0   21.0    5.0
>    55.0    8.0   31.0
> 
> 
> data<-read.table("d.txt",header=TRUE)
> mod<-lm(data$Y~data$X+data$Z)
> ---------------------------------------
> 
> I would like to know the predict value at a new level,
> say 
> 
> X=10 Z=30

> dat <- scan()
1:    42.0    7.0   33.0
4:    33.0    4.0   41.0
7:    75.0   16.0    7.0
10:    28.0    3.0   49.0
13:    91.0   21.0    5.0
16:    55.0    8.0   31.0
19:
Read 18 items
> dat <- as.data.frame(matrix(dat, ncol = 3, byrow = TRUE))
> names(dat) <- c("X","Y","Z")
> dat
   X  Y  Z
1 42  7 33
2 33  4 41
3 75 16  7
4 28  3 49
5 91 21  5
6 55  8 31
> mod <- lm(Y ~ X + Z, data = dat) # note use of data argument
> pred <- predict(mod, newdata = list(X = 10, Z = 30))
> pred
[1] -0.003469617

or

> pred <- predict(mod, newdata = data.frame(X = 10, Z = 30))
> pred
[1] -0.003469617

HTH

G

> 
> Is there a function available to calculate it
> directly?
> 
> Thank you
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 *Note new Address and Fax and Telephone numbers from 10th April 2006*
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/cv/
WC1E 6BT                          [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list