[R] Puzzled in utilising summary.lm() to obtain Var(x)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jun 15 11:29:23 CEST 2005


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> On Wed, 15 Jun 2005, Ajay Narottam Shah wrote:
> 
> >>> I have a program which is doing a few thousand runs of lm(). Suppose
> >>> it is a simple model
> >>>   y = a + bx1 + cx2 + e
> >>>
> >>> I have the R object "d" where
> >>>   d <- summary(lm(y ~ x1 + x2))
> >>>
> >>> I would like to obtain Var(x2) out of "d". How might I do it?
[snip]
> > var(model.matrix(d0)[,"Sepal.Width"])
> 
> but if you really only have the summary  (it's wasteful to keep the 
> summary here, as it is larger than the fit)
> 
> > var(model.matrix(structure(d, class="lm"))[,"Sepal.Width"])

Allow me to interject a little lateral thinking: 

   solve(vcov(d)[-1,-1]/d$sigma)[2,2]

should give the requested variance,I think.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list