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

2005-06-15 Thread Prof Brian Ripley
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?

 I can, of course, always do sd(x2). But it would be much more
 convenient if I could snoop around the contents of summary.lm and
 extract Var() out of it. I couldn't readily see how. Would you know
 what would click?

 Is the question how to get the variance of a column of the
 model matrix for a model that is the sum of terms given only
 summary output and the column name but not the name of the
 data frame?  If that is it then try this:

 d - summary(lm(Sepal.Length ~ Sepal.Width, iris)) # test data
 var(model.matrix(eval(d$call))[,Sepal.Width])

 Yes, this is indeed exactly what I was looking for :-) Thanks,

 The eval() pays the full cost of running d$call?

A better way is

 d0 - lm(Sepal.Length ~ Sepal.Width, iris)
 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])


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@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


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

2005-06-15 Thread Peter Dalgaard
Prof Brian Ripley [EMAIL PROTECTED] 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
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
R-help@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


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

2005-06-14 Thread Ajay Narottam Shah
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?

I can, of course, always do sd(x2). But it would be much more
convenient if I could snoop around the contents of summary.lm and
extract Var() out of it. I couldn't readily see how. Would you know
what would click?

-- 
Ajay Shah   Consultant
[EMAIL PROTECTED]  Department of Economic Affairs
http://www.mayin.org/ajayshah   Ministry of Finance, New Delhi

__
R-help@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


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

2005-06-14 Thread Gabor Grothendieck
On 6/14/05, Ajay Narottam Shah [EMAIL PROTECTED] 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?
 
 I can, of course, always do sd(x2). But it would be much more
 convenient if I could snoop around the contents of summary.lm and
 extract Var() out of it. I couldn't readily see how. Would you know
 what would click?
 


Is the question how to get the variance of a column of the
model matrix for a model that is the sum of terms given only
summary output and the column name but not the name of the
data frame?  If that is it then try this:

d - summary(lm(Sepal.Length ~ Sepal.Width, iris)) # test data
var(model.matrix(eval(d$call))[,Sepal.Width])

If that's not the question, try examining the contents of d using

str(d)

and examine the output of lm via

str(eval(d$call))

and perhaps that will suggest the answer.

__
R-help@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


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

2005-06-14 Thread Ajay Narottam Shah
  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?
  
  I can, of course, always do sd(x2). But it would be much more
  convenient if I could snoop around the contents of summary.lm and
  extract Var() out of it. I couldn't readily see how. Would you know
  what would click?
 
 Is the question how to get the variance of a column of the
 model matrix for a model that is the sum of terms given only
 summary output and the column name but not the name of the
 data frame?  If that is it then try this:
 
 d - summary(lm(Sepal.Length ~ Sepal.Width, iris)) # test data
 var(model.matrix(eval(d$call))[,Sepal.Width])

Yes, this is indeed exactly what I was looking for :-) Thanks,

The eval() pays the full cost of running d$call?

 -ans.

-- 
Ajay Shah   Consultant
[EMAIL PROTECTED]  Department of Economic Affairs
http://www.mayin.org/ajayshah   Ministry of Finance, New Delhi

__
R-help@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


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

2005-06-14 Thread Gabor Grothendieck
On 6/14/05, Ajay Narottam Shah [EMAIL PROTECTED] 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?
  
   I can, of course, always do sd(x2). But it would be much more
   convenient if I could snoop around the contents of summary.lm and
   extract Var() out of it. I couldn't readily see how. Would you know
   what would click?
 
  Is the question how to get the variance of a column of the
  model matrix for a model that is the sum of terms given only
  summary output and the column name but not the name of the
  data frame?  If that is it then try this:
 
  d - summary(lm(Sepal.Length ~ Sepal.Width, iris)) # test data
  var(model.matrix(eval(d$call))[,Sepal.Width])
 
 Yes, this is indeed exactly what I was looking for :-) Thanks,
 
 The eval() pays the full cost of running d$call?

Yes. It reruns it.  If we can assume that the second arg to lm is data=
then we could do this which simply grabs the indicated column from
the data frame:

f - function(d, name) eval(substitute(with(eval(d$call[[3]]), name)))
f(d, Sepal.Width)  # same as iris$Sepal.Width

# or

f - function(d, charname) eval(d$call[[3]])[[charname]]
f(d, Sepal.Width)

__
R-help@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