1
$\begingroup$

I have a dataset with several categorical predictors with varying factor levels. Is there a way to generate a correlation matrix from this data without having to create a bunch of dummy variables?

I'm using multiple linear regression to predict a continuous variable (sales). The predicted values are surprisingly accurate and plotting the predicted vs observed results in a near diagonal line.

I thought that was all I needed to worry about, but in researching, I found I should also plot predicted vs residuals to test for homoscedasticity. I did that and found out I was violating it.

I was looking for a way to resolve this and found a post that said I should use a robust method for computing the covariance matrix. Hence why I want to use the cor() function, though I’m not sure if that’s actually the right way of going about this.

And here are the actual graphs:

Predicted vs Actual...

Predicted vs Residual...

$\endgroup$
  • 1
    $\begingroup$ What do you want the correlation matrix for? What would be the problem w/ creating sets of dummy variables? $\endgroup$ – gung - Reinstate Monica Sep 5 '18 at 16:39
  • $\begingroup$ To start, I am extremely new to all of this, so forgive my ignorance. I'm using multiple linear regression to predict a continuous variable (sales). The predicted values are surprisingly accurate and plotting the predicted vs observed results in a near diagonal line. I thought that was all I needed to worry about, but in researching, I found I should also plot predicted vs residuals to test for homoscedasticity. I did that and found out I was violating it. I was looking for a way to resolve this and found a post that said I should use a robust method for computing the covariance matrix. $\endgroup$ – Charles Orlando Sep 5 '18 at 16:53
  • $\begingroup$ Hence why I want to use the cor function, though I’m not sure if that’s actually the right way of going about this. As for the problem with creating dummy sets, there’s no issue, and I’ll do that if there’s no other way. I was just curious if it was possible without doing that. @gung $\endgroup$ – Charles Orlando Sep 5 '18 at 16:54
  • 2
    $\begingroup$ This is good information. I suspect what you have here is an XY problem. It would be better to edit your Q to talk about your situation & what you are ultimately doing. (To foreshadow, I think you are going down the wrong path w/ trying to compute your own correlation matrix.) It might further help if you could paste in your data, or the plots that are troubling. $\endgroup$ – gung - Reinstate Monica Sep 5 '18 at 17:00
  • 2
    $\begingroup$ That doesn't look like heteroscedasticity to me; it looks like an outlier. $\endgroup$ – gung - Reinstate Monica Sep 5 '18 at 17:01
0
$\begingroup$

You're going to want to use the lmtest package for re-estimating the model along with the sandwich package for the robust covariance matrix

fit <- lm(sales ~ race + age + ...)

install.packages(sandwich)
install.packages(lmtest)
library(sandwich)
library(lmtest)

coeftest(fit, vcov = vcovHC(fit, type="HC"))

Type "HC" is the original White's estimator, the default in vcovHC is "HC3" and the reason for this is given in the documentation ?vcovHC

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.