I have a dataset that combines several surveys, with different countries, over years. My dependent variable (lrparty) is the ideological position of a party (ranging from 0 to 10) according to survey's respondents. I have several independent variables such as the age, sex, education, partisanship, and income of respondents.
Then, for each party and each survey, I would like to plot the predicted values of lrparty according to the modal individual (e.g., respondent with age = 31, female = 1, education = 2, income = 2, and partisan = 1) over time. So, the graph would look like: x-axis = years; y-axis = predicted values of lrparty according to the modal individual.
In sum, these are the stages of what I am trying to do: 1. Estimate the model: Ordered logit of placement of the party (lrparty), regressing on gender, age, education, income, and partisanship of respondents.
Take posterior draws.
Predict placement of the party for the modal respondent (e.g., 500 draws)
Then, I am expecting to have a dataset that should look like: Year, Survey, Country, Party (cmp code), %missing placements, x1:x500 (from the draws)
From that data set I would generate my plots. For example, for UK, according to survey CSES.
In order to figure it out the code, I started using only one survey (cses), one country (uk), and one party (conservatives) as you can see in my code below. But I don't know how to get from where I am in the code to the plot I want (above described).
library(rstan)
library(tidyverse)
library(brms)
library(ggplot2)
library(ggthemes)
library(ggmcmc)
## Data:
load("pbrands.RData")
## Keeping only country = uk; survey = cses; party = conservatives
uk_cses_con = pbrands %>%
select(lrparty, female, age, education, income, partisan, year, survey,
country, cmp, party_name_short, party_name_english, lrs) %>%
filter(survey == "cses") %>%
filter(country == "uk") %>%
filter(cmp == 51620)
## Conducting a Bayesian ordered logit model
fit <- brm(lrparty ~ age + income + education + female + partisan,
data = uk_cses_con, family = "cumulative", chains = 4, iter = 1000)
## Trace and Density Plots for MCMC Samples
plot(fit)
## Posterior Predictive Checks
pp_check(fit)
## Getting variables' modes:
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
getmode(uk_cses_con$age)
getmode(uk_cses_con$female)
getmode(uk_cses_con$education)
getmode(uk_cses_con$income)
getmode(uk_cses_con$partisan)
## Creating the data frame for the modal individual
newavg <- data.frame(age = 31, female = 1, education = 2, income = 2,
partisan = 0, years = uk_cses_con$year)
## predict response for new data
pred <- predict(fit, newdata = newavg)
# extract posterior samples of population-level effects
samples1 <- posterior_samples(fit)
## Display marginal effects of predictors
marginal <- marginal_effects(fit)
## Plot predicted lrparty (my dependent variable) over time (with error:
confidence interval) based on the modal respondent (age = 31, female = 1,
education = 0, income = 0, partisan = 0)
##?
Thanks in advance!