library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data <- data.frame(
id = 1:20,
diver = c('Alice', 'Alice', 'Alice', 'Bob', 'Bob', 'Bob', 'Charlie', 'Charlie', 'Charlie',
'Dave', 'Dave', 'Dave', 'Eve', 'Eve', 'Eve', 'Frank', 'Frank', 'Frank', 'Grace', 'Grace'),
dcountry = c('USA', 'USA', 'USA', 'CAN', 'CAN', 'CAN', 'RUS', 'RUS', 'RUS', 'MEX', 'MEX', 'MEX',
'FRA', 'FRA', 'FRA', 'GER', 'GER', 'GER', 'ITA', 'ITA'),
judge = c('William', 'Katherine', 'Jean', 'Fred', 'Nico',
'William', 'Katherine', 'Jean', 'Fred', 'Nico',
'William', 'Katherine', 'Jean', 'Fred', 'Nico',
'William', 'Katherine', 'Fred', 'Fred', 'Nico'),
jcountry = c('USA', 'RUS', 'FRA', 'GER', 'ITA',
'USA', 'RUS', 'FRA', 'GER', 'ITA',
'USA', 'RUS', 'FRA', 'GER', 'ITA',
'USA', 'RUS', 'GER', 'GER', 'ITA'),
score = c(7.5, 7.0, 6.5, 7.0, 7.8, 7.1, 7.3, 7.4, 6.9, 7.2,
7.0, 6.8, 7.6, 7.7, 7.1, 7.3, 7.2, 7.0, 7.4, 7.5)
)
print(data)
## id diver dcountry judge jcountry score
## 1 1 Alice USA William USA 7.5
## 2 2 Alice USA Katherine RUS 7.0
## 3 3 Alice USA Jean FRA 6.5
## 4 4 Bob CAN Fred GER 7.0
## 5 5 Bob CAN Nico ITA 7.8
## 6 6 Bob CAN William USA 7.1
## 7 7 Charlie RUS Katherine RUS 7.3
## 8 8 Charlie RUS Jean FRA 7.4
## 9 9 Charlie RUS Fred GER 6.9
## 10 10 Dave MEX Nico ITA 7.2
## 11 11 Dave MEX William USA 7.0
## 12 12 Dave MEX Katherine RUS 6.8
## 13 13 Eve FRA Jean FRA 7.6
## 14 14 Eve FRA Fred GER 7.7
## 15 15 Eve FRA Nico ITA 7.1
## 16 16 Frank GER William USA 7.3
## 17 17 Frank GER Katherine RUS 7.2
## 18 18 Frank GER Fred GER 7.0
## 19 19 Grace ITA Fred GER 7.4
## 20 20 Grace ITA Nico ITA 7.5
data <- data %>%
mutate(same_country = ifelse(dcountry == jcountry, 1, 0))
model <- lm(score ~ diver + judge + same_country:jcountry, data = data)
summary(model)
##
## Call:
## lm(formula = score ~ diver + judge + same_country:jcountry, data = data)
##
## Residuals:
## 1 2 3 4 5 6 7
## -1.719e-17 2.277e-01 -2.277e-01 -1.988e-01 3.483e-01 -1.495e-01 -8.073e-17
## 8 9 10 11 12 13 14
## 2.277e-01 -2.277e-01 7.812e-02 8.024e-02 -1.584e-01 -7.379e-17 4.264e-01
## 15 16 17 18 19 20
## -4.264e-01 6.930e-02 -6.930e-02 3.030e-17 -1.828e-17 9.479e-18
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.68298 0.54236 12.322 0.000249 ***
## diverBob 0.51581 0.54739 0.942 0.399391
## diverCharlie 0.44468 0.51411 0.865 0.435856
## diverDave 0.18602 0.50695 0.367 0.732256
## diverEve 0.59058 0.59943 0.985 0.380288
## diverFrank 0.49696 0.53158 0.935 0.402782
## diverGrace 0.71702 0.70322 1.020 0.365557
## judgeJean 0.04468 0.51411 0.087 0.934921
## judgeKatherine 0.08936 0.50575 0.177 0.868337
## judgeNico 0.25289 0.40850 0.619 0.569387
## judgeWilliam 0.05076 0.46693 0.109 0.918668
## same_country:jcountryFRA 0.28176 0.74526 0.378 0.724585
## same_country:jcountryGER -0.17994 0.70408 -0.256 0.810891
## same_country:jcountryITA -0.15289 0.75339 -0.203 0.849093
## same_country:jcountryRUS 0.08298 0.70322 0.118 0.911757
## same_country:jcountryUSA 0.76626 0.69450 1.103 0.331800
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4476 on 4 degrees of freedom
## Multiple R-squared: 0.5922, Adjusted R-squared: -0.9369
## F-statistic: 0.3873 on 15 and 4 DF, p-value: 0.9202
(I fixed to include the other terms, judge, and diver, in the model, which I didn’t do on the exam)
data <- data %>%
mutate(same_country_w_country = ifelse(dcountry == jcountry, dcountry, "Not Same"))
data$same_country_w_country <- factor(data$same_country_w_country, levels = c("Not Same", unique(data$dcountry)))
model2 <- lm(score ~ diver + judge + same_country_w_country, data = data)
summary(model2)
##
## Call:
## lm(formula = score ~ diver + judge + same_country_w_country,
## data = data)
##
## Residuals:
## 1 2 3 4 5 6 7
## 1.425e-17 2.277e-01 -2.277e-01 -1.988e-01 3.483e-01 -1.495e-01 -7.968e-18
## 8 9 10 11 12 13 14
## 2.277e-01 -2.277e-01 7.812e-02 8.024e-02 -1.584e-01 -9.470e-17 4.264e-01
## 15 16 17 18 19 20
## -4.264e-01 6.930e-02 -6.930e-02 2.673e-17 -3.572e-17 -7.968e-18
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.68298 0.54236 12.322 0.000249 ***
## diverBob 0.51581 0.54739 0.942 0.399391
## diverCharlie 0.44468 0.51411 0.865 0.435856
## diverDave 0.18602 0.50695 0.367 0.732256
## diverEve 0.59058 0.59943 0.985 0.380288
## diverFrank 0.49696 0.53158 0.935 0.402782
## diverGrace 0.71702 0.70322 1.020 0.365557
## judgeJean 0.04468 0.51411 0.087 0.934921
## judgeKatherine 0.08936 0.50575 0.177 0.868337
## judgeNico 0.25289 0.40850 0.619 0.569387
## judgeWilliam 0.05076 0.46693 0.109 0.918668
## same_country_w_countryUSA 0.76626 0.69450 1.103 0.331800
## same_country_w_countryRUS 0.08298 0.70322 0.118 0.911757
## same_country_w_countryFRA 0.28176 0.74526 0.378 0.724585
## same_country_w_countryGER -0.17994 0.70408 -0.256 0.810891
## same_country_w_countryITA -0.15289 0.75339 -0.203 0.849093
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4476 on 4 degrees of freedom
## Multiple R-squared: 0.5922, Adjusted R-squared: -0.9369
## F-statistic: 0.3873 on 15 and 4 DF, p-value: 0.9202