Using mutate to read in the variances and the covariances of a linear model using mutate

I am currently working on a huge data set and would like to compute the variance for a combination of my parameter estimates. I would like to read in data from the vcov matrix into a data set and then compute the variance. I am using tidyverse and in particular I am using mutate to try and read in the variance covariance components. I have created a toy data set due to the confidentiality issues for my data.

mod <- lm(Gas ~ Temp +Insul+0, whiteside)
#model summary
#variance covariance matrix
# Now I want to read in the variance covariance matrix: To obtain the variance 
# of say a linear combination of some parameters: Beta_Temp + Beta_InsulBefore  
# Now I have a bew dataset: data2 where I want to use Covariate1:Covariate3 as 
# indices to obtain my covariances.
data2 <-"Temp","InsulBefore","InsulAfter"),
# I get the variances 
data3<- data2 |>  mutate(Variances=diag(vcov(mod))[Covariate1])
# I have issues trying to get the covariances
data4<- data3 |>  mutate(Cov12=(vcov(mod)[Covariate1,Covariate2]))  
## Cov12 is a matrix but I want it to be a column with only values from the 
## covariance of Temp:InsulBefore, InsulBefore:InsulAfter and InsulAfter:Temp. 
## This is just a toy data as I can't share the real data for confidentiality 
## reasons. 

a hint since this seems like homework, look at the elements mod$ has, maybe there is a covariance matrix in there.

Notice that is a matrix of text, not sure why you defined it like that.

I created the to just mimic the real data that I have. The idea is that I want to get the variance for each of the variables and the covariance for the different variables

The issue is not about getting the variance covariance matrix but how I can read the variance covariance elements into my data set

once you found the variance matrix in the LM model you can either give names to the columns and then use dplyr language (mutate for example) to import data by columns, or you can use normal matrix indexing as in m[i,j] for single cell, m[ ,j], m[i, ] for single column or row.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.