3
$\begingroup$

Dataset

Consider the following dataset which measures the price of a computer given different configurations

Situation

After applying the four-way classification model as below, we have the summary output of the model and the relevant coefficients:

fullwithinteract <- lm(Price ~CPU + Speed + Floppy + Monitor + CPU:Speed + CPU:Floppy + CPU:Monitor + Speed:Floppy + Floppy:Monitor,Q10set1)

Problem

The problem arises when I tried to reproduce the coefficients output by R using a design matrix. I first consider the following design matrix:

enter image description here

And I find the coefficients by the normal equation

solve(t(X)%*%X) %*% t(X) %*% Y

where $X$ is the design matrix above and $Y$ is the Price response vector.

The output is        
V1   4688.9286
V2  -2872.5000
V3  -1388.9286
V4   2326.0714
V5    482.5000
V6   1252.2619
V7   -742.5000
V8    837.7381
V9  -2126.0714
V10    17.5000

which is drastically different from the coefficients output by R, EXCEPT in some entries. Therefore, how should I interpret the coefficients output by R? Is R calculating the coefficients in the same way as I did using the normal equation? Should I even use the normal equation to calculate the coefficients?

Data availability

All the data and the design matrix used in the above presentation can be obtained in the following website: https://www.notion.so/hephaes/Data-Coefficients-by-R-e161d467a09948028a43633aaf616229

$\endgroup$

1 Answer 1

2
$\begingroup$

You can get the design matrix from R formula

Here is an example.

> model.matrix(mpg~wt+wt*cyl,mtcars)
                    (Intercept)    wt cyl wt:cyl
Mazda RX4                     1 2.620   6 15.720
Mazda RX4 Wag                 1 2.875   6 17.250
Datsun 710                    1 2.320   4  9.280
Hornet 4 Drive                1 3.215   6 19.290
Hornet Sportabout             1 3.440   8 27.520
Valiant                       1 3.460   6 20.760

Try to set

X = model.matrix(Price ~CPU + Speed + Floppy + Monitor + CPU:Speed + CPU:Floppy + CPU:Monitor + Speed:Floppy + Floppy:Monitor,Q10set1)

Then use normal equation to solve.

$\endgroup$
1
  • $\begingroup$ Thank you so much. A simple check reveals that in the original dataset CPU, Speed, Floppy are identified NOT as factors and therefore the estimates are not reasonable. After modifying the columns of the original dataset to be factors and after using your suggestion the results are now fully reproducible. Thanks! $\endgroup$
    – hephaes
    May 13, 2020 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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