Example
This example uses national election data, which has a lot of columns of data for every election from 1952 to 2000. For this example we are only concerned with the 1992 election and the impact of income on preference between Bush (y=1) and Bill Clinton.
The code below is modified from the
tidyros
repo.
file_nes <- here::here("data/nes.txt")
nes <-
file_nes %>%
read.table() %>%
as_tibble() %>%
select(year, income, dvote, rvote) %>%
filter(xor(dvote, rvote)) %>% # only those with pref
filter(year == 1992)
nes |> count(dvote, rvote)
## # A tibble: 2 × 3
## dvote rvote n
## <int> <int> <int>
## 1 0 1 477
## 2 1 0 702
Logistic regression :
set.seed(660)
fit_1 <-
stan_glm(
rvote ~ income,
family = binomial(link = "logit"), # this make it logistic
data = nes,
refresh = 0 # Supress rows of updates
)
fit_1
## stan_glm
## family: binomial [logit]
## formula: rvote ~ income
## observations: 1179
## predictors: 2
## ------
## Median MAD_SD
## (Intercept) -1.4 0.2
## income 0.3 0.1
##
## ------
## * For help interpreting the printed output see ?print.stanreg
## * For info on the priors used see ?prior_summary.stanreg
Lets plot it;