Project

General

Profile

Support #1770 ยป ethnicity_standard_errors_reprex.R

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

 
1
library(survey)
2
library(srvyr)
3
library(readstata13)
4
library(dplyr)
5
library(tidyr)
6

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

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

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

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

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

    
34

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

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

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

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

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