#Making dummy codes; Dummy coded regression

#First, we need to convert our nominal variable into a labelled character factor
#so we'll better be able to understand our output

Wk10nm$Group2 = factor(Wk10nm$Group,
levels = c(1,2,3,4,5),
labels = c("Grp1","Grp2","Grp3","Grp4","Grp5"))


#install.packages('fastDummies')
library(fastDummies)

# Dummy code MARSTAT2
Wk10nm2 <- dummy_cols(Wk10nm, select_columns = c("Group2"))


###Regression of DV1 on FOUR Group2 dummies,
# CHOOSING to leave one out ("reference category"), Group2_Grp1

fit5 <- lm(DV1 ~ Group2_Grp2+Group2_Grp3+Group2_Grp4+Group2_Grp5, data=Wk10nm2)
summary(fit5) # show results
## 
## Call:
## lm(formula = DV1 ~ Group2_Grp2 + Group2_Grp3 + Group2_Grp4 +
## Group2_Grp5, data = Wk10nm2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -36.988 -11.219 -0.262 9.909 39.930
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 38.333 2.885 13.285 < 2e-16 ***
## Group2_Grp2 13.321 3.587 3.714 0.000267 ***
## Group2_Grp3 8.258 3.515 2.350 0.019798 *
## Group2_Grp4 15.070 3.860 3.904 0.000130 ***
## Group2_Grp5 27.600 4.998 5.523 1.06e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.8 on 195 degrees of freedom
## Multiple R-squared: 0.1582, Adjusted R-squared: 0.1409
## F-statistic: 9.159 on 4 and 195 DF, p-value: 8.414e-07
confint(fit5, level=0.95) # CIs for model parameters 
##                 2.5 %   97.5 %
## (Intercept) 32.642787 44.02388
## Group2_Grp2 6.246934 20.39549
## Group2_Grp3 1.326168 15.18996
## Group2_Grp4 7.457862 22.68249
## Group2_Grp5 17.743684 37.45632
lm.beta(fit5)
## Group2_Grp2 Group2_Grp3 Group2_Grp4 Group2_Grp5 
## 0.3497256 0.2245598 0.3476050 0.4274241