Skip to content

Instantly share code, notes, and snippets.

@FFFiend
Created March 5, 2024 06:55
Show Gist options
  • Save FFFiend/be103c71022abc2712dd812072eeef85 to your computer and use it in GitHub Desktop.
Save FFFiend/be103c71022abc2712dd812072eeef85 to your computer and use it in GitHub Desktop.
qz83.R
# Set seed for reproducibility
set.seed(123)
# Number of observations
n <- 1000
# Simulate age group (categorical with 4 levels)
age_group <- sample(1:4, n, replace = TRUE)
# Simulate gender (binary: 0 = male, 1 = female)
gender <- sample(0:1, n, replace = TRUE)
# Simulate income group (categorical with 5 levels)
income_group <- sample(1:5, n, replace = TRUE)
# Simulate highest education (categorical with 3 levels)
education <- sample(1:3, n, replace = TRUE)
# Simulate predictors associated with count of levels
predictor_1 <- rpois(n, lambda = age_group)
predictor_2 <- rpois(n, lambda = income_group)
predictor_3 <- rpois(n, lambda = education)
# Simulate political party support (binary: 0 = no, 1 = yes)
support <- rbinom(n, size = 1, prob = 0.5)
# Create a data frame
data <- data.frame(age_group, gender, income_group, education, predictor_1, predictor_2, predictor_3, support)
library(ggplot2)
# Create a scatterplot of political party support vs. age group
ggplot(data, aes(x = age_group, y = support)) +
geom_jitter(width = 0.2, height = 0.1) +
labs(x = "Age Group", y = "Political Party Support (Yes/No)") +
ggtitle("Political Party Support by Age Group") +
theme_minimal()
library(rstanarm)
# Fit Bayesian logistic regression model with political party support as the response and age group as the predictor
model <- stan_glm(support ~ age_group, data = data, family = "binomial")
# Print model summary
print(model)
@FFFiend
Copy link
Author

FFFiend commented Mar 5, 2024

CHATGPT HISTORY

User
nono, take this CODE

Set seed for reproducibility

set.seed(123)

Number of observations

n <- 1000

Simulate age group (categorical with 4 levels)

age_group <- sample(1:4, n, replace = TRUE)

Simulate gender (binary: 0 = male, 1 = female)

gender <- sample(0:1, n, replace = TRUE)

Simulate income group (categorical with 5 levels)

income_group <- sample(1:5, n, replace = TRUE)

Simulate highest education (categorical with 3 levels)

education <- sample(1:3, n, replace = TRUE)

Simulate predictors associated with count of levels

predictor_1 <- rpois(n, lambda = age_group)
predictor_2 <- rpois(n, lambda = income_group)
predictor_3 <- rpois(n, lambda = education)

Simulate political party support (binary: 0 = no, 1 = yes)

support <- rbinom(n, size = 1, prob = 0.5)

Create a data frame

data <- data.frame(age_group, gender, income_group, education, predictor_1, predictor_2, predictor_3, support)

Perform tests based on the simulated data

Example tests:

Test 1: Chi-square test for association between age group and political party support

chisq.test(table(data$age_group, data$support))

Test 2: Logistic regression for political party support with gender as predictor

summary(glm(support ~ gender, data = data, family = "binomial"))

Test 3: ANOVA for association between income group and political party support

summary(aov(support ~ income_group, data = data))

Test 4: Logistic regression for political party support with education as predictor

summary(glm(support ~ education, data = data, family = "binomial"))

Test 5: Chi-square test for association between predictor 1 and political party support

chisq.test(table(data$predictor_1, data$support))

Test 6: Logistic regression for political party support with predictor 2 as predictor

summary(glm(support ~ predictor_2, data = data, family = "binomial"))

Test 7: ANOVA for association between predictor 3 and political party support

summary(aov(support ~ predictor_3, data = data))

Test 8: Chi-square test for association between gender and political party support

chisq.test(table(data$gender, data$support))

Test 9: Logistic regression for political party support with age group as predictor

summary(glm(support ~ age_group, data = data, family = "binomial"))

Test 10: ANOVA for association between predictor 1 and political party support

summary(aov(support ~ predictor_1, data = data))

and THEN, off of THIS prompt "Support for a political party is a binary (yes/no), and is related to age-group, gender, income group, and highest education.", u do the following:
Please use ggplot2 to build the graph that you sketched. Then use rstanarm to build the model.

ChatGPT

To visualize the relationship between political party support and one of the predictor variables from the simulated data using ggplot2, let's create a scatterplot. We'll plot the relationship between political party support and one of the predictor variables, say age group.

R

Load the ggplot2 package

library(ggplot2)

Create a scatterplot of political party support vs. age group

ggplot(data, aes(x = age_group, y = support)) +
geom_jitter(width = 0.2, height = 0.1) +
labs(x = "Age Group", y = "Political Party Support (Yes/No)") +
ggtitle("Political Party Support by Age Group") +
theme_minimal()

This code will create a scatterplot with age group on the x-axis and political party support (binary) on the y-axis. Each point represents an observation, with jitter added to avoid overplotting.

Now, to build a Bayesian logistic regression model using rstanarm, we'll use the same simulated data and fit a model with political party support as the response variable and age group as the predictor.

R

Load the rstanarm package

library(rstanarm)

Fit Bayesian logistic regression model with political party support as the response and age group as the predictor

model <- stan_glm(support ~ age_group, data = data, family = "binomial")

Print model summary

print(model)

This code will fit a Bayesian logistic regression model using Stan via the rstanarm package. It estimates the relationship between political party support and age group while accounting for uncertainty using a Bayesian framework. You can print the model summary to examine the estimated coefficients, credible intervals, and other diagnostic information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment