1. Selection of variables/Model comparison

For the kanga data set, recall we selected just the data for which there was no missing values

library(faraway)
## Warning: package 'faraway' was built under R version 4.0.3
data(kanga)
kanga2 <- kanga[!is.na(rowSums(kanga[,3:20])),]



#This time, let's try to predict zygomatic.width with the other predictors.

## a. Model selection with ANOVA.
# Build a model predicting zygomatic width with every other predictor.  Use anova() to compare this model to smaller models, iteratively removing one variable at a time, until you have the smallest model you think is justified by the ANOVA test.  Then, compare that model to the full model again and determine whether it really is no different from the full model.  Describe the model and justify why you would use it.


model = lm(zygomatic.width~basilar.length+occipitonasal.length+palate.length+palate.width+nasal.length+nasal.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model,test="F")

model2 = lm(zygomatic.width~occipitonasal.length+palate.length+palate.width+nasal.length+nasal.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model2,test="F")


model3 = lm(zygomatic.width~palate.length+palate.width+nasal.length+nasal.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model3,test="F")


model4 = lm(zygomatic.width~palate.width+nasal.length+nasal.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model4,test="F")


model5 = lm(zygomatic.width~palate.width+nasal.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model5,test="F")

model6 = lm(zygomatic.width~palate.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model6,test="F")

model7 = lm(zygomatic.width~palate.width+squamosal.depth+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model7,test="F")

model8 = lm(zygomatic.width~palate.width+squamosal.depth+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model8,test="F")

model9 = lm(zygomatic.width~palate.width+squamosal.depth+.rostral.width+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)


#drop1(model9,test="F")


model10 = lm(zygomatic.width~palate.width+squamosal.depth+.rostral.width+crest.width+mandible.length+mandible.width+mandible.depth+ramus.height+sex, data=kanga2)

#drop1(model10,test="F")

model11 = lm(zygomatic.width~palate.width+squamosal.depth+.rostral.width+crest.width+mandible.length+mandible.width+ramus.height+sex, data=kanga2)

#drop1(model11,test="F")


model12 = lm(zygomatic.width~palate.width+squamosal.depth+.rostral.width+crest.width+mandible.length+mandible.width+ramus.height, data=kanga2)

#drop1(model12,test="F")

# I have removed variables one by one not in order of magnitude of p but from top to down logic

anova(model,model2,model3,model4,model5,model6,model7,model8,model9,model10,model11,model12) # Here model 12 seems to be the best one to pick. ANOVA is comparing all models subsequently and the model 12 does not significantly differ from model 11 (p>0.05)
## Analysis of Variance Table
## 
## Model  1: zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + nasal.width + squamosal.depth + 
##     lacrymal.width + orbital.width + .rostral.width + occipital.depth + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## Model  2: zygomatic.width ~ occipitonasal.length + palate.length + palate.width + 
##     nasal.length + nasal.width + squamosal.depth + lacrymal.width + 
##     orbital.width + .rostral.width + occipital.depth + crest.width + 
##     foramina.length + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## Model  3: zygomatic.width ~ palate.length + palate.width + nasal.length + 
##     nasal.width + squamosal.depth + lacrymal.width + orbital.width + 
##     .rostral.width + occipital.depth + crest.width + foramina.length + 
##     mandible.length + mandible.width + mandible.depth + ramus.height + 
##     sex
## Model  4: zygomatic.width ~ palate.width + nasal.length + nasal.width + 
##     squamosal.depth + lacrymal.width + orbital.width + .rostral.width + 
##     occipital.depth + crest.width + foramina.length + mandible.length + 
##     mandible.width + mandible.depth + ramus.height + sex
## Model  5: zygomatic.width ~ palate.width + nasal.width + squamosal.depth + 
##     lacrymal.width + orbital.width + .rostral.width + occipital.depth + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## Model  6: zygomatic.width ~ palate.width + squamosal.depth + lacrymal.width + 
##     orbital.width + .rostral.width + occipital.depth + crest.width + 
##     foramina.length + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## Model  7: zygomatic.width ~ palate.width + squamosal.depth + orbital.width + 
##     .rostral.width + occipital.depth + crest.width + foramina.length + 
##     mandible.length + mandible.width + mandible.depth + ramus.height + 
##     sex
## Model  8: zygomatic.width ~ palate.width + squamosal.depth + .rostral.width + 
##     occipital.depth + crest.width + foramina.length + mandible.length + 
##     mandible.width + mandible.depth + ramus.height + sex
## Model  9: zygomatic.width ~ palate.width + squamosal.depth + .rostral.width + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## Model 10: zygomatic.width ~ palate.width + squamosal.depth + .rostral.width + 
##     crest.width + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## Model 11: zygomatic.width ~ palate.width + squamosal.depth + .rostral.width + 
##     crest.width + mandible.length + mandible.width + ramus.height + 
##     sex
## Model 12: zygomatic.width ~ palate.width + squamosal.depth + .rostral.width + 
##     crest.width + mandible.length + mandible.width + ramus.height
##    Res.Df   RSS Df Sum of Sq      F Pr(>F)
## 1      82 22837                           
## 2      83 22992 -1   -155.20 0.5573 0.4575
## 3      84 23004 -1    -12.18 0.0438 0.8348
## 4      85 23139 -1   -135.13 0.4852 0.4880
## 5      86 23465 -1   -325.94 1.1704 0.2825
## 6      87 23610 -1   -144.80 0.5199 0.4729
## 7      88 23718 -1   -108.36 0.3891 0.5345
## 8      89 23725 -1     -6.24 0.0224 0.8814
## 9      90 23950 -1   -225.05 0.8081 0.3713
## 10     91 24555 -1   -605.38 2.1738 0.1442
## 11     92 24991 -1   -435.71 1.5645 0.2146
## 12     93 25566 -1   -575.61 2.0668 0.1543
library(MASS)
## Warning: package 'MASS' was built under R version 4.0.3
stepAIC_selected_model = stepAIC(model,direction = 'both')
## Start:  AIC=585.52
## zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + nasal.width + squamosal.depth + 
##     lacrymal.width + orbital.width + .rostral.width + occipital.depth + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - nasal.width           1       1.8 22838 583.53
## - orbital.width         1       3.3 22840 583.54
## - occipitonasal.length  1       9.8 22846 583.56
## - occipital.depth       1     116.1 22953 584.03
## - lacrymal.width        1     118.8 22955 584.04
## - basilar.length        1     155.2 22992 584.21
## - nasal.length          1     238.9 23076 584.57
## - sex                   1     275.9 23113 584.73
## - palate.length         1     281.2 23118 584.76
## - mandible.depth        1     325.8 23162 584.95
## <none>                              22837 585.52
## - foramina.length       1     510.2 23347 585.75
## - mandible.width        1    1225.7 24062 588.80
## - .rostral.width        1    1305.6 24142 589.14
## - mandible.length       1    1426.2 24263 589.64
## - squamosal.depth       1    1587.2 24424 590.31
## - palate.width          1    2174.3 25011 592.71
## - ramus.height          1    2466.0 25303 593.88
## - crest.width           1    6163.0 29000 607.65
## 
## Step:  AIC=583.53
## zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + squamosal.depth + lacrymal.width + 
##     orbital.width + .rostral.width + occipital.depth + crest.width + 
##     foramina.length + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - orbital.width         1       3.3 22842 581.54
## - occipitonasal.length  1       9.6 22848 581.57
## - occipital.depth       1     117.9 22956 582.05
## - lacrymal.width        1     149.9 22988 582.19
## - basilar.length        1     153.6 22992 582.21
## - sex                   1     277.9 23116 582.75
## - nasal.length          1     286.3 23125 582.79
## - palate.length         1     287.2 23126 582.79
## - mandible.depth        1     360.7 23199 583.11
## <none>                              22838 583.53
## - foramina.length       1     510.9 23349 583.76
## + nasal.width           1       1.8 22837 585.52
## - mandible.width        1    1236.4 24075 586.85
## - .rostral.width        1    1305.2 24144 587.14
## - mandible.length       1    1427.2 24266 587.65
## - squamosal.depth       1    1586.1 24425 588.31
## - palate.width          1    2340.9 25179 591.38
## - ramus.height          1    2983.4 25822 593.93
## - crest.width           1    6169.6 29008 605.68
## 
## Step:  AIC=581.54
## zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + squamosal.depth + lacrymal.width + 
##     .rostral.width + occipital.depth + crest.width + foramina.length + 
##     mandible.length + mandible.width + mandible.depth + ramus.height + 
##     sex
## 
##                        Df Sum of Sq   RSS    AIC
## - occipitonasal.length  1       8.1 22850 579.58
## - occipital.depth       1     118.0 22960 580.06
## - lacrymal.width        1     149.5 22991 580.20
## - basilar.length        1     160.4 23002 580.25
## - sex                   1     276.0 23118 580.76
## - nasal.length          1     283.1 23125 580.79
## - palate.length         1     292.7 23134 580.83
## - mandible.depth        1     375.9 23218 581.19
## <none>                              22842 581.54
## - foramina.length       1     545.4 23387 581.93
## + orbital.width         1       3.3 22838 583.53
## + nasal.width           1       1.8 22840 583.54
## - mandible.width        1    1267.3 24109 585.00
## - .rostral.width        1    1330.7 24172 585.26
## - mandible.length       1    1470.7 24312 585.85
## - squamosal.depth       1    1680.0 24522 586.71
## - palate.width          1    2347.1 25189 589.42
## - ramus.height          1    2981.9 25824 591.94
## - crest.width           1    7023.6 29865 606.62
## 
## Step:  AIC=579.58
## zygomatic.width ~ basilar.length + palate.length + palate.width + 
##     nasal.length + squamosal.depth + lacrymal.width + .rostral.width + 
##     occipital.depth + crest.width + foramina.length + mandible.length + 
##     mandible.width + mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - occipital.depth       1     110.9 22961 578.07
## - basilar.length        1     171.8 23022 578.34
## - lacrymal.width        1     182.1 23032 578.38
## - palate.length         1     285.2 23135 578.83
## - sex                   1     338.1 23188 579.06
## - mandible.depth        1     388.0 23238 579.28
## <none>                              22850 579.58
## - nasal.length          1     496.2 23346 579.75
## - foramina.length       1     558.2 23408 580.02
## + occipitonasal.length  1       8.1 22842 581.54
## + orbital.width         1       1.7 22848 581.57
## + nasal.width           1       1.7 22848 581.57
## - mandible.width        1    1260.9 24111 583.00
## - .rostral.width        1    1331.3 24181 583.30
## - mandible.length       1    1463.8 24314 583.85
## - squamosal.depth       1    1671.9 24522 584.71
## - palate.width          1    2363.6 25213 587.52
## - ramus.height          1    2987.3 25837 589.99
## - crest.width           1    7020.3 29870 604.64
## 
## Step:  AIC=578.07
## zygomatic.width ~ basilar.length + palate.length + palate.width + 
##     nasal.length + squamosal.depth + lacrymal.width + .rostral.width + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - lacrymal.width        1     125.0 23086 576.62
## - basilar.length        1     153.3 23114 576.74
## - sex                   1     292.7 23253 577.35
## - palate.length         1     295.1 23256 577.36
## - mandible.depth        1     381.6 23342 577.73
## <none>                              22961 578.07
## - foramina.length       1     575.2 23536 578.57
## - nasal.length          1     692.8 23654 579.07
## + occipital.depth       1     110.9 22850 579.58
## + nasal.width           1       3.4 22957 580.05
## + orbital.width         1       2.7 22958 580.06
## + occipitonasal.length  1       1.1 22960 580.06
## - mandible.width        1    1161.8 24123 581.05
## - .rostral.width        1    1334.6 24295 581.77
## - mandible.length       1    1373.2 24334 581.93
## - squamosal.depth       1    1588.7 24549 582.83
## - palate.width          1    2284.9 25246 585.65
## - ramus.height          1    2876.4 25837 587.99
## - crest.width           1    7048.5 30009 603.11
## 
## Step:  AIC=576.62
## zygomatic.width ~ basilar.length + palate.length + palate.width + 
##     nasal.length + squamosal.depth + .rostral.width + crest.width + 
##     foramina.length + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - basilar.length        1     135.7 23221 575.21
## - palate.length         1     304.6 23390 575.94
## - mandible.depth        1     339.2 23425 576.09
## - sex                   1     346.5 23432 576.12
## <none>                              23086 576.62
## - foramina.length       1     479.0 23565 576.69
## - nasal.length          1     589.2 23675 577.16
## + lacrymal.width        1     125.0 22961 578.07
## + occipital.depth       1      53.8 23032 578.38
## + nasal.width           1      28.5 23057 578.49
## + occipitonasal.length  1      20.1 23066 578.53
## + orbital.width         1       5.3 23080 578.59
## - mandible.width        1    1209.6 24295 579.77
## - mandible.length       1    1346.3 24432 580.34
## - squamosal.depth       1    1654.1 24740 581.61
## - .rostral.width        1    1758.2 24844 582.03
## - palate.width          1    2431.4 25517 584.73
## - ramus.height          1    3066.2 26152 587.21
## - crest.width           1    7905.4 30991 604.36
## 
## Step:  AIC=575.21
## zygomatic.width ~ palate.length + palate.width + nasal.length + 
##     squamosal.depth + .rostral.width + crest.width + foramina.length + 
##     mandible.length + mandible.width + mandible.depth + ramus.height + 
##     sex
## 
##                        Df Sum of Sq   RSS    AIC
## - palate.length         1     175.3 23397 573.97
## - sex                   1     261.7 23483 574.34
## - foramina.length       1     418.0 23639 575.01
## <none>                              23221 575.21
## - mandible.depth        1     470.0 23691 575.23
## - nasal.length          1     713.9 23935 576.27
## + basilar.length        1     135.7 23086 576.62
## + lacrymal.width        1     107.4 23114 576.74
## + occipital.depth       1      45.1 23176 577.01
## + nasal.width           1      31.7 23190 577.07
## + occipitonasal.length  1       4.1 23217 577.19
## + orbital.width         1       0.2 23221 577.21
## - mandible.length       1    1218.0 24439 578.37
## - mandible.width        1    1252.4 24474 578.51
## - .rostral.width        1    1638.8 24860 580.10
## - squamosal.depth       1    1750.0 24971 580.55
## - ramus.height          1    2944.9 26166 585.27
## - palate.width          1    3188.3 26410 586.20
## - crest.width           1    7772.2 30994 602.37
## 
## Step:  AIC=573.97
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + foramina.length + mandible.length + 
##     mandible.width + mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - foramina.length       1     375.1 23772 573.57
## - sex                   1     384.5 23781 573.61
## <none>                              23397 573.97
## - mandible.depth        1     489.0 23886 574.06
## - nasal.length          1     552.8 23950 574.33
## + palate.length         1     175.3 23221 575.21
## + lacrymal.width        1     127.6 23269 575.42
## + occipital.depth       1      55.9 23341 575.73
## + nasal.width           1      20.3 23376 575.88
## + basilar.length        1       6.5 23390 575.94
## + occipitonasal.length  1       0.6 23396 575.97
## + orbital.width         1       0.2 23397 575.97
## - mandible.width        1    1387.0 24784 577.79
## - .rostral.width        1    1689.5 25086 579.01
## - squamosal.depth       1    1836.0 25233 579.60
## - ramus.height          1    2839.6 26236 583.54
## - palate.width          1    3077.6 26474 584.45
## - mandible.length       1    3547.5 26944 586.23
## - crest.width           1    7597.1 30994 600.37
## 
## Step:  AIC=573.57
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - mandible.depth        1     413.6 24185 573.32
## - sex                   1     439.7 24212 573.43
## <none>                              23772 573.57
## + foramina.length       1     375.1 23397 573.97
## - nasal.length          1     783.1 24555 574.85
## + palate.length         1     132.4 23639 575.01
## + occipital.depth       1      88.3 23684 575.20
## + lacrymal.width        1      37.5 23734 575.42
## + orbital.width         1      15.6 23756 575.51
## + occipitonasal.length  1       2.4 23769 575.56
## + nasal.width           1       1.8 23770 575.57
## + basilar.length        1       1.3 23771 575.57
## - mandible.width        1    1427.6 25200 577.47
## - .rostral.width        1    1672.9 25445 578.44
## - squamosal.depth       1    1695.7 25468 578.53
## - ramus.height          1    2952.7 26725 583.40
## - palate.width          1    2983.4 26755 583.52
## - mandible.length       1    3538.2 27310 585.59
## - crest.width           1    7436.9 31209 599.07
## 
## Step:  AIC=573.32
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - sex                   1     405.3 24591 573.00
## <none>                              24185 573.32
## + mandible.depth        1     413.6 23772 573.57
## + foramina.length       1     299.7 23886 574.06
## - nasal.length          1     805.2 24991 574.62
## + palate.length         1     151.7 24034 574.68
## + occipital.depth       1      85.5 24100 574.96
## + orbital.width         1      53.9 24132 575.09
## + basilar.length        1      23.8 24162 575.22
## + lacrymal.width        1      16.7 24169 575.25
## + nasal.width           1      14.5 24171 575.26
## + occipitonasal.length  1       0.0 24185 575.32
## - mandible.width        1    1207.0 25393 576.24
## - squamosal.depth       1    1419.4 25605 577.08
## - .rostral.width        1    1618.7 25804 577.86
## - ramus.height          1    2612.8 26798 581.68
## - mandible.length       1    3257.0 27442 584.08
## - palate.width          1    3526.7 27712 585.06
## - crest.width           1    7087.7 31273 597.27
## 
## Step:  AIC=573
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     ramus.height
## 
##                        Df Sum of Sq   RSS    AIC
## <none>                              24591 573.00
## + sex                   1     405.3 24185 573.32
## + mandible.depth        1     379.2 24212 573.43
## + foramina.length       1     350.0 24241 573.55
## + palate.length         1     267.5 24323 573.89
## + occipitonasal.length  1      86.5 24504 574.64
## + lacrymal.width        1      48.6 24542 574.80
## + occipital.depth       1      40.4 24550 574.83
## - nasal.length          1     975.5 25566 574.92
## + basilar.length        1       4.8 24586 574.98
## + nasal.width           1       0.8 24590 574.99
## + orbital.width         1       0.3 24590 574.99
## - squamosal.depth       1    1421.5 26012 576.67
## - mandible.width        1    1546.1 26137 577.15
## - .rostral.width        1    1579.7 26170 577.28
## - ramus.height          1    2351.3 26942 580.22
## - palate.width          1    3499.1 28090 584.43
## - mandible.length       1    3678.0 28269 585.07
## - crest.width           1    6704.3 31295 595.35
stepAIC_selected_model$call #this is the chosen model by stepwise regression of AIC
## lm(formula = zygomatic.width ~ palate.width + nasal.length + 
##     squamosal.depth + .rostral.width + crest.width + mandible.length + 
##     mandible.width + ramus.height, data = kanga2)
BIC_model = stepAIC(model,direction = 'both',k = log(length(model$residuals)))
## Start:  AIC=635.21
## zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + nasal.width + squamosal.depth + 
##     lacrymal.width + orbital.width + .rostral.width + occipital.depth + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - nasal.width           1       1.8 22838 630.60
## - orbital.width         1       3.3 22840 630.61
## - occipitonasal.length  1       9.8 22846 630.64
## - occipital.depth       1     116.1 22953 631.11
## - lacrymal.width        1     118.8 22955 631.12
## - basilar.length        1     155.2 22992 631.28
## - nasal.length          1     238.9 23076 631.64
## - sex                   1     275.9 23113 631.81
## - palate.length         1     281.2 23118 631.83
## - mandible.depth        1     325.8 23162 632.02
## - foramina.length       1     510.2 23347 632.82
## <none>                              22837 635.21
## - mandible.width        1    1225.7 24062 635.87
## - .rostral.width        1    1305.6 24142 636.21
## - mandible.length       1    1426.2 24263 636.71
## - squamosal.depth       1    1587.2 24424 637.38
## - palate.width          1    2174.3 25011 639.78
## - ramus.height          1    2466.0 25303 640.95
## - crest.width           1    6163.0 29000 654.72
## 
## Step:  AIC=630.6
## zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + squamosal.depth + lacrymal.width + 
##     orbital.width + .rostral.width + occipital.depth + crest.width + 
##     foramina.length + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - orbital.width         1       3.3 22842 626.00
## - occipitonasal.length  1       9.6 22848 626.03
## - occipital.depth       1     117.9 22956 626.51
## - lacrymal.width        1     149.9 22988 626.65
## - basilar.length        1     153.6 22992 626.66
## - sex                   1     277.9 23116 627.21
## - nasal.length          1     286.3 23125 627.24
## - palate.length         1     287.2 23126 627.25
## - mandible.depth        1     360.7 23199 627.57
## - foramina.length       1     510.9 23349 628.22
## <none>                              22838 630.60
## - mandible.width        1    1236.4 24075 631.31
## - .rostral.width        1    1305.2 24144 631.60
## - mandible.length       1    1427.2 24266 632.11
## - squamosal.depth       1    1586.1 24425 632.77
## + nasal.width           1       1.8 22837 635.21
## - palate.width          1    2340.9 25179 635.84
## - ramus.height          1    2983.4 25822 638.39
## - crest.width           1    6169.6 29008 650.14
## 
## Step:  AIC=626
## zygomatic.width ~ basilar.length + occipitonasal.length + palate.length + 
##     palate.width + nasal.length + squamosal.depth + lacrymal.width + 
##     .rostral.width + occipital.depth + crest.width + foramina.length + 
##     mandible.length + mandible.width + mandible.depth + ramus.height + 
##     sex
## 
##                        Df Sum of Sq   RSS    AIC
## - occipitonasal.length  1       8.1 22850 621.42
## - occipital.depth       1     118.0 22960 621.91
## - lacrymal.width        1     149.5 22991 622.04
## - basilar.length        1     160.4 23002 622.09
## - sex                   1     276.0 23118 622.60
## - nasal.length          1     283.1 23125 622.63
## - palate.length         1     292.7 23134 622.67
## - mandible.depth        1     375.9 23218 623.03
## - foramina.length       1     545.4 23387 623.77
## <none>                              22842 626.00
## - mandible.width        1    1267.3 24109 626.84
## - .rostral.width        1    1330.7 24172 627.10
## - mandible.length       1    1470.7 24312 627.69
## - squamosal.depth       1    1680.0 24522 628.55
## + orbital.width         1       3.3 22838 630.60
## + nasal.width           1       1.8 22840 630.61
## - palate.width          1    2347.1 25189 631.26
## - ramus.height          1    2981.9 25824 633.78
## - crest.width           1    7023.6 29865 648.46
## 
## Step:  AIC=621.42
## zygomatic.width ~ basilar.length + palate.length + palate.width + 
##     nasal.length + squamosal.depth + lacrymal.width + .rostral.width + 
##     occipital.depth + crest.width + foramina.length + mandible.length + 
##     mandible.width + mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - occipital.depth       1     110.9 22961 617.30
## - basilar.length        1     171.8 23022 617.56
## - lacrymal.width        1     182.1 23032 617.61
## - palate.length         1     285.2 23135 618.06
## - sex                   1     338.1 23188 618.29
## - mandible.depth        1     388.0 23238 618.51
## - nasal.length          1     496.2 23346 618.98
## - foramina.length       1     558.2 23408 619.24
## <none>                              22850 621.42
## - mandible.width        1    1260.9 24111 622.23
## - .rostral.width        1    1331.3 24181 622.53
## - mandible.length       1    1463.8 24314 623.08
## - squamosal.depth       1    1671.9 24522 623.94
## + occipitonasal.length  1       8.1 22842 626.00
## + orbital.width         1       1.7 22848 626.03
## + nasal.width           1       1.7 22848 626.03
## - palate.width          1    2363.6 25213 626.75
## - ramus.height          1    2987.3 25837 629.22
## - crest.width           1    7020.3 29870 643.87
## 
## Step:  AIC=617.3
## zygomatic.width ~ basilar.length + palate.length + palate.width + 
##     nasal.length + squamosal.depth + lacrymal.width + .rostral.width + 
##     crest.width + foramina.length + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - lacrymal.width        1     125.0 23086 613.23
## - basilar.length        1     153.3 23114 613.35
## - sex                   1     292.7 23253 613.96
## - palate.length         1     295.1 23256 613.97
## - mandible.depth        1     381.6 23342 614.34
## - foramina.length       1     575.2 23536 615.18
## - nasal.length          1     692.8 23654 615.68
## <none>                              22961 617.30
## - mandible.width        1    1161.8 24123 617.67
## - .rostral.width        1    1334.6 24295 618.39
## - mandible.length       1    1373.2 24334 618.55
## - squamosal.depth       1    1588.7 24549 619.44
## + occipital.depth       1     110.9 22850 621.42
## + nasal.width           1       3.4 22957 621.90
## + orbital.width         1       2.7 22958 621.90
## + occipitonasal.length  1       1.1 22960 621.91
## - palate.width          1    2284.9 25246 622.26
## - ramus.height          1    2876.4 25837 624.60
## - crest.width           1    7048.5 30009 639.72
## 
## Step:  AIC=613.23
## zygomatic.width ~ basilar.length + palate.length + palate.width + 
##     nasal.length + squamosal.depth + .rostral.width + crest.width + 
##     foramina.length + mandible.length + mandible.width + mandible.depth + 
##     ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - basilar.length        1     135.7 23221 609.21
## - palate.length         1     304.6 23390 609.94
## - mandible.depth        1     339.2 23425 610.09
## - sex                   1     346.5 23432 610.12
## - foramina.length       1     479.0 23565 610.69
## - nasal.length          1     589.2 23675 611.16
## <none>                              23086 613.23
## - mandible.width        1    1209.6 24295 613.77
## - mandible.length       1    1346.3 24432 614.34
## - squamosal.depth       1    1654.1 24740 615.60
## - .rostral.width        1    1758.2 24844 616.03
## + lacrymal.width        1     125.0 22961 617.30
## + occipital.depth       1      53.8 23032 617.61
## + nasal.width           1      28.5 23057 617.72
## + occipitonasal.length  1      20.1 23066 617.76
## + orbital.width         1       5.3 23080 617.82
## - palate.width          1    2431.4 25517 618.73
## - ramus.height          1    3066.2 26152 621.21
## - crest.width           1    7905.4 30991 638.36
## 
## Step:  AIC=609.21
## zygomatic.width ~ palate.length + palate.width + nasal.length + 
##     squamosal.depth + .rostral.width + crest.width + foramina.length + 
##     mandible.length + mandible.width + mandible.depth + ramus.height + 
##     sex
## 
##                        Df Sum of Sq   RSS    AIC
## - palate.length         1     175.3 23397 605.35
## - sex                   1     261.7 23483 605.72
## - foramina.length       1     418.0 23639 606.39
## - mandible.depth        1     470.0 23691 606.61
## - nasal.length          1     713.9 23935 607.65
## <none>                              23221 609.21
## - mandible.length       1    1218.0 24439 609.75
## - mandible.width        1    1252.4 24474 609.90
## - .rostral.width        1    1638.8 24860 611.48
## - squamosal.depth       1    1750.0 24971 611.93
## + basilar.length        1     135.7 23086 613.23
## + lacrymal.width        1     107.4 23114 613.35
## + occipital.depth       1      45.1 23176 613.62
## + nasal.width           1      31.7 23190 613.68
## + occipitonasal.length  1       4.1 23217 613.80
## + orbital.width         1       0.2 23221 613.82
## - ramus.height          1    2944.9 26166 616.65
## - palate.width          1    3188.3 26410 617.58
## - crest.width           1    7772.2 30994 633.75
## 
## Step:  AIC=605.35
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + foramina.length + mandible.length + 
##     mandible.width + mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - foramina.length       1     375.1 23772 602.34
## - sex                   1     384.5 23781 602.38
## - mandible.depth        1     489.0 23886 602.82
## - nasal.length          1     552.8 23950 603.09
## <none>                              23397 605.35
## - mandible.width        1    1387.0 24784 606.55
## - .rostral.width        1    1689.5 25086 607.78
## - squamosal.depth       1    1836.0 25233 608.36
## + palate.length         1     175.3 23221 609.21
## + lacrymal.width        1     127.6 23269 609.41
## + occipital.depth       1      55.9 23341 609.72
## + nasal.width           1      20.3 23376 609.88
## + basilar.length        1       6.5 23390 609.94
## + occipitonasal.length  1       0.6 23396 609.96
## + orbital.width         1       0.2 23397 609.96
## - ramus.height          1    2839.6 26236 612.30
## - palate.width          1    3077.6 26474 613.22
## - mandible.length       1    3547.5 26944 614.99
## - crest.width           1    7597.1 30994 629.13
## 
## Step:  AIC=602.34
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     mandible.depth + ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - mandible.depth        1     413.6 24185 599.47
## - sex                   1     439.7 24212 599.58
## - nasal.length          1     783.1 24555 601.00
## <none>                              23772 602.34
## - mandible.width        1    1427.6 25200 603.62
## - .rostral.width        1    1672.9 25445 604.59
## - squamosal.depth       1    1695.7 25468 604.69
## + foramina.length       1     375.1 23397 605.35
## + palate.length         1     132.4 23639 606.39
## + occipital.depth       1      88.3 23684 606.58
## + lacrymal.width        1      37.5 23734 606.80
## + orbital.width         1      15.6 23756 606.89
## + occipitonasal.length  1       2.4 23769 606.95
## + nasal.width           1       1.8 23770 606.95
## + basilar.length        1       1.3 23771 606.95
## - ramus.height          1    2952.7 26725 609.55
## - palate.width          1    2983.4 26755 609.67
## - mandible.length       1    3538.2 27310 611.74
## - crest.width           1    7436.9 31209 625.22
## 
## Step:  AIC=599.47
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     ramus.height + sex
## 
##                        Df Sum of Sq   RSS    AIC
## - sex                   1     405.3 24591 596.53
## - nasal.length          1     805.2 24991 598.16
## <none>                              24185 599.47
## - mandible.width        1    1207.0 25393 599.77
## - squamosal.depth       1    1419.4 25605 600.61
## - .rostral.width        1    1618.7 25804 601.40
## + mandible.depth        1     413.6 23772 602.34
## + foramina.length       1     299.7 23886 602.82
## + palate.length         1     151.7 24034 603.45
## + occipital.depth       1      85.5 24100 603.73
## + orbital.width         1      53.9 24132 603.86
## + basilar.length        1      23.8 24162 603.98
## + lacrymal.width        1      16.7 24169 604.01
## + nasal.width           1      14.5 24171 604.02
## + occipitonasal.length  1       0.0 24185 604.08
## - ramus.height          1    2612.8 26798 605.21
## - mandible.length       1    3257.0 27442 607.61
## - palate.width          1    3526.7 27712 608.60
## - crest.width           1    7087.7 31273 620.81
## 
## Step:  AIC=596.53
## zygomatic.width ~ palate.width + nasal.length + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     ramus.height
## 
##                        Df Sum of Sq   RSS    AIC
## - nasal.length          1     975.5 25566 595.85
## <none>                              24591 596.53
## - squamosal.depth       1    1421.5 26012 597.59
## - mandible.width        1    1546.1 26137 598.07
## - .rostral.width        1    1579.7 26170 598.20
## + sex                   1     405.3 24185 599.47
## + mandible.depth        1     379.2 24212 599.58
## + foramina.length       1     350.0 24241 599.70
## + palate.length         1     267.5 24323 600.04
## + occipitonasal.length  1      86.5 24504 600.79
## + lacrymal.width        1      48.6 24542 600.95
## + occipital.depth       1      40.4 24550 600.98
## + basilar.length        1       4.8 24586 601.13
## - ramus.height          1    2351.3 26942 601.14
## + nasal.width           1       0.8 24590 601.14
## + orbital.width         1       0.3 24590 601.15
## - palate.width          1    3499.1 28090 605.35
## - mandible.length       1    3678.0 28269 605.99
## - crest.width           1    6704.3 31295 616.27
## 
## Step:  AIC=595.85
## zygomatic.width ~ palate.width + squamosal.depth + .rostral.width + 
##     crest.width + mandible.length + mandible.width + ramus.height
## 
##                        Df Sum of Sq   RSS    AIC
## <none>                              25566 595.85
## + nasal.length          1     975.5 24591 596.53
## - .rostral.width        1    1506.9 27073 597.01
## + foramina.length       1     615.5 24951 598.00
## + sex                   1     575.6 24991 598.16
## - squamosal.depth       1    1944.6 27511 598.63
## + mandible.depth        1     396.0 25170 598.88
## + occipitonasal.length  1     313.1 25253 599.22
## + occipital.depth       1     280.7 25285 599.35
## + nasal.width           1     267.8 25298 599.40
## + orbital.width         1      53.1 25513 600.25
## + basilar.length        1      47.3 25519 600.27
## + palate.length         1      17.5 25549 600.39
## + lacrymal.width        1      15.0 25551 600.40
## - mandible.length       1    2779.2 28345 601.65
## - mandible.width        1    3005.0 28571 602.45
## - ramus.height          1    3150.5 28717 602.97
## - palate.width          1    3586.0 29152 604.49
## - crest.width           1    8743.1 34309 620.94
BIC_model$call 
## lm(formula = zygomatic.width ~ palate.width + squamosal.depth + 
##     .rostral.width + crest.width + mandible.length + mandible.width + 
##     ramus.height, data = kanga2)
#this is the chosen model by stepwise regression of BIC that introduces a penalty term for the number of parameters in the model, and the unneeded variable is nasal.length in our case


# we need to get rid of the categorical variable by converting it in numbers
library(BayesFactor)
## Warning: package 'BayesFactor' was built under R version 4.0.2
## Loading required package: coda
## Warning: package 'coda' was built under R version 4.0.2
## Loading required package: Matrix
## ************
## Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
kanga2$sexnumeric = as.numeric(kanga2$sex)

# options('BFMaxModels' = 270000)
# modelBF = regressionBF(zygomatic.width~basilar.length+occipitonasal.length+palate.length+palate.width+nasal.length+nasal.width+squamosal.depth+lacrymal.width+orbital.width+.rostral.width+occipital.depth+crest.width+foramina.length+mandible.length+mandible.width+mandible.depth+ramus.height+sexnumeric,data=kanga2)
#plot(head(modelBF))

best_model_BF = lm(zygomatic.width~palate.width+nasal.length+crest.width+mandible.length+ramus.height, data = kanga2)

#Palate.width, Nasal.length, Crest.width, Mandible.length, Ramus.height are the 5 best variables to predict zygomatic.width when using a Bayesian Factor Analysis



par(mfrow=c(2,2))
#ANOVA

par(font=4)
plot(kanga2$zygomatic.width,col='blue',pch = 16, cex = 1,ylab = 'Zygomatic Width',xlab= 'Number of Kangaroos',ylim = c(700,1100))
points(model12$fitted.values,col='red',cex = 1,pch=10)
segments(1:101,kanga2$zygomatic.width,1:101,model12$fitted.values,lty=3,lwd=.9)
legend(20,1090,legend=c('True Values','Fitted Values'),col=c('blue','red'),pch=c(16,10), cex=0.8)
title('ANOVA Model')
text(60,710,sprintf('r = %s adjr^2=0.940',round(cor(kanga2$zygomatic.width,model12$fitted.values),3)))

#stepwise AIC

par(font=4)
plot(kanga2$zygomatic.width,col='orange',pch = 16, cex = 1,ylab = 'Zygomatic Width',xlab= 'Number of Kangaroos',ylim = c(700,1100))
points(stepAIC_selected_model$fitted.values,col='grey',cex = 1,pch=10)
segments(1:101,kanga2$zygomatic.width,1:101,stepAIC_selected_model$fitted.values,lty=3,lwd=.9)
legend(20,1090,legend=c('True Values','Fitted Values'),col=c('orange','grey'),pch=c(16,10), cex=0.8)
title('Stepwise AIC Model Selection')
text(60,710,sprintf('r = %s  adjr^2=0.941',round(cor(kanga2$zygomatic.width,stepAIC_selected_model$fitted.values),3)))

#stepwise BIC

par(font=4)
plot(kanga2$zygomatic.width,col='violet',pch = 16, cex = 1,ylab = 'Zygomatic Width',xlab= 'Number of Kangaroos',ylim = c(700,1100))
points(BIC_model$fitted.values,col='black',cex = 1,pch=10)
segments(1:101,kanga2$zygomatic.width,1:101,BIC_model$fitted.values,lty=3,lwd=.9)
legend(20,1090,legend=c('True Values','Fitted Values'),col=c('violet','black'),pch=c(16,10), cex=0.8)
title('Stepwise BIC Model Selection')
text(60,710,sprintf('r = %s adjr^2=0.940',round(cor(kanga2$zygomatic.width,BIC_model$fitted.values),3)))

# regressionBF

par(font=4)
plot(kanga2$zygomatic.width,col='green',pch = 16, cex = 1,ylab = 'Zygomatic Width',xlab= 'Number of Kangaroos',ylim = c(700,1100))
points(best_model_BF$fitted.values,col='purple',cex = 1,pch=10)
segments(1:101,kanga2$zygomatic.width,1:101,best_model_BF$fitted.values,lty=3,lwd=.9)
legend(20,1090,legend=c('True Values','Fitted Values'),col=c('green','purple'),pch=c(16,10), cex=0.8)
title('Bayesian Factor Analysis Model Selection')
text(60,710,sprintf('r = %s adjr^2=0.938',round(cor(kanga2$zygomatic.width,best_model_BF$fitted.values),3)))

#If I were to choose between these 4 models the Stepwise AIC one seems to have a slight advantage over the other ones although the Bayesian Factor seems to work very well with only 5 variables and this could be an advantage also for data collection.


par(font=4)
plot(kanga2$zygomatic.width,col='black',pch = 8, cex = 1.1,ylab = 'Zygomatic Width',xlab= 'Number of Kangaroos',ylim = c(700,1100))
points(best_model_BF$fitted.values,col='purple',cex = 0.8,pch=10)
points(BIC_model$fitted.values,col='green',cex = 1.2,pch=10)
points(stepAIC_selected_model$fitted.values,col='yellow',cex = 0.8,pch=10)
points(model12$fitted.values,col='red',cex = 0.8,pch=10)
legend(20,1090,legend=c('True Values','FITTED BF','FITTED BIC','FITTED AIC','FITTED ANOVA'),col=c('black','purple','green','yellow','red'),pch=c(8,10,10,10,10), cex=0.8)
title('All models comparison')



#calculation time needed for regressionBF 

n = 21
number_of_permutations = 0
permutation = 0
for (i in 1:n){
  permutation = factorial(n)/(factorial(n - i)*factorial(i))
  number_of_permutations = number_of_permutations + permutation 
  print(number_of_permutations)
}
## [1] 21
## [1] 231
## [1] 1561
## [1] 7546
## [1] 27895
## [1] 82159
## [1] 198439
## [1] 401929
## [1] 695859
## [1] 1048575
## [1] 1401291
## [1] 1695221
## [1] 1898711
## [1] 2014991
## [1] 2069255
## [1] 2089604
## [1] 2095589
## [1] 2096919
## [1] 2097129
## [1] 2097150
## [1] 2097151
average_time_ms_per_model = 7200 / number_of_permutations * 1000 #it took 2 hours for the process to finish
average_time_ms_per_model
## [1] 3.433229

2. Orthogonal Designs

In an new experiment, you have two 3-level independent variables: * training (no training, standard training, interactive training) * feedback (no feedback, feedback on errors, feedback on correct)

training <- rep(c(1,1,1,0,0,0,-1,-1,-1),each=3)
feedback <- rep(c(1,0,-1,1,0,-1,1,0,-1),each=3)

cor(training, feedback)
## [1] 0
training %*% feedback
##      [,1]
## [1,]    0
# group = sample(1:3,length(training),replace = T)
# group_norm = group - mean(group)
# group%*%training
# group%*%feedback

set.seed(100)
group1 <- 1:27
group2 <- rep(-1:1, 9)
group3 <- 1:27-mean(1:27)
group4 <- rep(1:3, 9)
group5 <- rep(-1:1,each=9)
group6 <- runif(27)-.5
group7 <- rep(1:9,each=3)

c = data.frame(group1,group2,group3,group4,group5,group6,group7)
groups <- data.frame(cor_feedback=rep(NA,7),
                           cor_training=rep(NA,7),
                           cross_product_feedback=rep(NA,7),
                           cross_product_training=rep(NA,7))
                    
for (i in 1:7){
  
  groups[i,1] = cor(c[i],feedback)
  groups[i,2]= cor(c[i],training)
  groups[i,3] = c[[i]] %*% feedback
  groups[i,4]= c[[i]] %*% training
  
}

sorted_groups = groups[rowSums(groups) == 0,]

#only groups 2 and 4 are orthogonal and not correlated to training and feedback. This is because our variables are structured by 3 main repetitions and by 9 other "divisions" which can be of every nature as long as they satisfy this structure.If we were to create any sort of random numbers repeated by 3 we will get the same result as shown here below. Thus, they will cancel out since at one value will always corresponde another "matching" value for all instances, thus variables are totally independent by one another. Since, if we take one point in space by two independent axes like (3,5) and slide through the y-axis (3,8) our x-axis will not change because it's independent by y.

group8 = rep(c(5,4,10),9)

cor(group8,feedback)
## [1] 0
cor(group8,training)
## [1] 0
group8 %*% feedback
##      [,1]
## [1,]    0
group8 %*% training
##      [,1]
## [1,]    0

2. Regression with orthogonal and uncorrelated predictors.

data <- data.frame(buyerid = group7,
                   timeframe=group4)

set.seed(103)

buyerbase <- runif(9)
timebase <- c(1,2,2.5)
agebase  <- 20+runif(9)* 40

data$age <- round(agebase[data$buyerid]) #basically repeat each number 3 times
data$purchase <- round( 25 + buyerbase[data$buyerid] * 50 +
                          data$age * .5+ 
                          timebase[data$timeframe] * 7 +
                          rnorm(27)*4,2)

data <- read.table(text=
"row  buyerid timeframe age purchase
1        1         1  33    57.80
2        1         2  33    62.57
3        1         3  33    69.93
4        2         1  38    54.65
5        2         2  38    71.48
6        2         3  38    63.58
7        3         1  31    70.04
8        3         2  31    82.43
9        3         3  31    84.55
10       4         1  52    82.26
11       4         2  52    93.35
12       4         3  52    97.40
13       5         1  53    65.57
14       5         2  53    70.54
15       5         3  53    69.12
16       6         1  55    59.10
17       6         2  55    73.83
18       6         3  55    72.33
19       7         1  25    70.92
20       7         2  25    76.99
21       7         3  25    77.22
22       8         1  26    50.54
23       8         2  26    64.44
24       8         3  26    67.53
25       9         1  31    50.79
26       9         2  31    56.01
27       9         3  31    57.03",header=T)


non_ort_model = lm(purchase~timeframe+age,data = data)
lm(purchase~timeframe,data = data)
## 
## Call:
## lm(formula = purchase ~ timeframe, data = data)
## 
## Coefficients:
## (Intercept)    timeframe  
##       58.55         5.39
lm(purchase~age,data = data)
## 
## Call:
## lm(formula = purchase ~ age, data = data)
## 
## Coefficients:
## (Intercept)          age  
##       57.10         0.32
#the slopes remain the same but the intercepts change accordingly. The intercept changes accordingly to the variables because it is basically the value of the predicted variable at predictors = 0.

data$ort_timeframe = data$timeframe - mean(data$timeframe)
data$ort_age = data$age - mean(data$age)

data$ort_timeframe %*% data$ort_age
##      [,1]
## [1,]    0
cor(data$ort_timeframe,data$ort_age) #they are orthogonal and uncorrelated now
## [1] 0
ort_model = lm(purchase~ort_timeframe+ort_age,data = data)
#their slopes do not change but the intercept does because it is now in relation to the mean value of the predictors and not when they = 0.

# For the second set of models, give an explanation of how to interpret each coefficient of the orthogonal predictors, and especially explain how it differs from your interpretation for the original predictors.
library(knitr)
## Warning: package 'knitr' was built under R version 4.0.3
library(rgl)
## Warning: package 'rgl' was built under R version 4.0.3
knit_hooks$set(webgl = hook_webgl)
coefs <- coef(ort_model)
a <- coefs["ort_timeframe"]
b <- coefs["ort_age"]
c <- coefs["(Intercept)"]

plot3d(data$ort_age,data$purchase,data$ort_timeframe, xlab = 'Orthogonal Age', ylab = 'Purchase cost', zlab = 'Orthogonal Timeframe',type = 'n')
spheres3d(data$ort_age,data$purchase,data$ort_timeframe,col='blue',radius = 0.5,alpha = 0.3)
spheres3d(data$ort_age,ort_model$fitted.values,data$ort_timeframe,col = 'yellow', radius = 0.5,alpha = 0.3)
planes3d(a, b, c, alpha = 0.5)

#this is a 3d plot of our true and fitted values using our orthogonal predictors. The plane is defined by the coefficients of those two predictors and the intercept is meaningful at mean predictors = 0

You must enable Javascript to view this page properly.