W
by using our method of using W
to explain our other variables, then take the residualsW
library(gapminder)
data(gapminder)
cor(gapminder$lifeExp,log(gapminder$gdpPercap))
## [1] 0.8076179
gapminder <- gapminder %>% group_by(country) %>%
mutate(lifeExp.r = lifeExp - mean(lifeExp),
logGDP.r = log(gdpPercap) - mean(log(gdpPercap))) %>% ungroup()
cor(gapminder$lifeExp.r,gapminder$logGDP.r)
## [1] 0.6404051
head(mmdata)
## # A tibble: 6 x 6
## state year assault robbery pop1000 sentreform
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 "ALA " 70 7413 1731 3450 0
## 2 "ALA " 71 7645 2005 3497 0
## 3 "ALA " 72 7431 2407 3540 0
## 4 "ALA " 73 8362 2809 3581 0
## 5 "ALA " 74 8429 3562 3628 0
## 6 "ALA " 75 8440 4446 3681 0
mmdata <- mmdata %>% mutate(assaultper1000 = assault/pop1000,
robberyper1000 = robbery/pop1000)
reform -> robberies
and reform <- state -> robberies
, which is something we can address with fixed effects.cor(mmdata$sentreform,mmdata$robberyper1000)
## [1] 0.1351003
mmdata <- mmdata %>% group_by(state) %>%
mutate(reform.m = sentreform-mean(sentreform),
robbery.m = robberyper1000-mean(robberyper1000))
cor(mmdata$reform.m,mmdata$robbery.m)
## [1] 0.2482301
teacher
on the test score
s of high school studentsparents' intelligence
, age
, demographics
, school
, last year's teacher
parents' intelligence
, demographics
, and school
, but leave open age
and last year's teacher