Ajay Narottam Shah
2005-Jun-14 13:03 UTC
[R] Puzzled in utilising summary.lm() to obtain Var(x)
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 ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
Gabor Grothendieck
2005-Jun-14 21:59 UTC
[R] Puzzled in utilising summary.lm() to obtain Var(x)
On 6/14/05, Ajay Narottam Shah <ajayshah at mayin.org> 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.