Project

General

Profile

Support #1770 ยป ethnicity_standard_errors_reprex.R

Laurence Rowley-Abel, 10/11/2022 01:39 PM

 
library(survey)
library(srvyr)
library(readstata13)
library(dplyr)
library(tidyr)

# Load dataset
path_to_ukhls_folder<- "C:/Users/laure/OneDrive/Documents/AIM CISC Project/Data/UKDA-6614-stata/stata/stata13_se/ukhls"
setwd(path_to_ukhls_folder)
main_df<- read.dta13("j_indresp.dta", generate.factors = T, convert.factors = T, nonint.factors = T)

# Created weighted and unweighted survey design objects
options(survey.lonely.psu="remove")
weighted_svy_df<- as_survey_design(main_df, ids = j_psu, strata = j_strata, weights = j_indinus_lw)
unweighted_svy_df<- as_survey_design(main_df, ids = j_psu, strata = j_strata)

# Created grouped ethnicity variable for both weighted and unweighted data
ethn_levels<- levels(main_df$j_ethn_dv)

weighted_svy_df<- weighted_svy_df%>%
mutate(ethnic_group = ifelse(j_ethn_dv %in% ethn_levels[c(2,14,11)], "white",
ifelse(j_ethn_dv %in% ethn_levels[c(3,4,5,6,19)], "asian",
ifelse(j_ethn_dv %in% ethn_levels[c(7,8,9)], "black",
ifelse(j_ethn_dv == "missing", "missing",
"other")))))

unweighted_svy_df<- unweighted_svy_df%>%
mutate(ethnic_group = ifelse(j_ethn_dv %in% ethn_levels[c(2,14,11)], "white",
ifelse(j_ethn_dv %in% ethn_levels[c(3,4,5,6,19)], "asian",
ifelse(j_ethn_dv %in% ethn_levels[c(7,8,9)], "black",
ifelse(j_ethn_dv == "missing", "missing",
"other")))))


# Create age variable for both weighted and unweighted data
weighted_svy_df<- weighted_svy_df%>%
mutate(age = as.numeric(as.character(j_age_dv)))

unweighted_svy_df<- unweighted_svy_df%>%
mutate(age = as.numeric(as.character(j_age_dv)))

# Calculate mean age for black respondents for both weighted and unweighted data
svymean(~age, subset(weighted_svy_df, ethnic_group == "black"))

svymean(~age, subset(unweighted_svy_df, ethnic_group == "black"))

# SE for weighted data is 0.9789
# SE for unweighted data is 0.5051
    (1-1/1)