Skip to content

Instantly share code, notes, and snippets.

@FFFiend
Last active March 5, 2024 07:01
Show Gist options
  • Save FFFiend/31b55afc06f08cdf2224c64383bc68d5 to your computer and use it in GitHub Desktop.
Save FFFiend/31b55afc06f08cdf2224c64383bc68d5 to your computer and use it in GitHub Desktop.
qz8.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)
# 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))
@FFFiend
Copy link
Author

FFFiend commented Mar 5, 2024

Chat history with CHATGPT

User
Support for a political party is a binary (yes/no), and is related to age-group, gender, income group, and highest education

(Simulate) Please further consider the scenario described and simulate the situation, along with three predictors that are associated the count of the number of levels. Please include at least ten tests based on the simulated data.

ChatGPT

To simulate the scenario described, I'll generate synthetic data using R. I'll consider political party support as a binary outcome (yes/no) and include age group, gender, income group, and highest education as predictors. Additionally, I'll simulate three predictors associated with the count of the number of levels. Then, I'll perform ten tests based on the simulated data.

Here's the R code to accomplish this:

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)

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))

This code generates synthetic data for the scenario described, including age group, gender, income group, and highest education as predictors. It also simulates three additional predictors associated with the count of levels. Then, it performs ten tests based on the simulated data, including chi-square tests, logistic regression, and ANOVA tests, to assess the association between predictors and political party support.

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