[R] Calculation of VCV matrix of estimated coefficient

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Fri Sep 6 10:46:36 CEST 2024



> On 5 Sep 2024, at 16:36 , Gabor Grothendieck <ggrothendieck using gmail.com> wrote:
> 
> sigma(model)^2 will give the correct MSE.  Also note that your model
> matrix has intercept at
> the end whereas vcov will have it at the beginning so you will need to
> permute the rows
> and columns to get them to be the same/


Also, 

X <- cbind(1, as.matrix(mtcars[, c('disp', 'hp')]))

or even

X <- model.matrix(~ disp + hp, mtcars)

-pd


> 
> On Wed, Sep 4, 2024 at 3:34 PM Daniel Lobo <danielobo9976 using gmail.com> wrote:
>> 
>> Hi,
>> 
>> I am trying to replicate the R's result for VCV matrix of estimated
>> coefficients from linear model as below
>> 
>> data(mtcars)
>> model <- lm(mpg~disp+hp, data=mtcars)
>> model_summ <-summary(model)
>> MSE = mean(model_summ$residuals^2)
>> vcov(model)
>> 
>> Now I want to calculate the same thing manually,
>> 
>> library(dplyr)
>> X = as.matrix(mtcars[, c('disp', 'hp')] %>% mutate(Intercept = 1));
>> solve(t(X) %*% X) * MSE
>> 
>> Unfortunately they do not match.
>> 
>> Could you please help where I made mistake, if any.
>> 
>> Thanks
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> -- 
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list