Homework 1
library(readr)
gro <- read_csv("C:/Users/gpe637/eic2015/eic2015csv/TR_PERSONA12.CSV",
na = "NA")
## Parsed with column specification:
## cols(
## .default = col_integer(),
## ID_VIV = col_double(),
## ID_PERSONA = col_double(),
## NOM_ENT = col_character(),
## MUN = col_character(),
## NOM_MUN = col_character(),
## LOC50K = col_character(),
## NOM_LOC = col_character(),
## ESTRATO = col_character(),
## UPM = col_character(),
## ENT_PAIS_NAC = col_character(),
## QDIALECT_C = col_character(),
## QDIALECT_INALI = col_character(),
## MUN_ASI = col_character(),
## NOM_MUN_ASI = col_character(),
## ENT_PAIS_ASI = col_character(),
## MED_TRASLADO_ESC3 = col_character(),
## MUN_RES10 = col_character(),
## NOM_MUN_RES10 = col_character(),
## ENT_PAIS_RES10 = col_character(),
## MUN_TRAB = col_character()
## # ... with 2 more columns
## )
## See spec(...) for full column specifications.
library(car)
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.1. https://CRAN.R-project.org/package=stargazer
library(survey)
## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
##
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
##
## dotchart
library(questionr)
I change the variable names to lower case
names<-names(gro)
newnames<-tolower(x=names)
names(gro)<-newnames
I will use the Intercensus Survey 2015 from Mexico to explore the relationship between the afrodescendant condition (outcome variable), sex, education and work activity in the state of Guerrero since historically in this state the afromexican population has long presence.
The following tables show the distribution of sex, education and income by afrodescendant condition in Guerrero. The variables where recode for the population older than 18 as: afromexican, not afromexican; male, female; No education, less than high school, high school, more than high school; work last week, student, retired or home working, not work last week.
gro<-subset(gro, edad>18)
gro$afromx<-recode(gro$afrodes, recodes = "1:2='Afromexican'; 3:8='No afromexican'; else=NA" )
gro$sex<-recode(gro$sexo, recodes = "1='Male'; 3='Female'")
gro$educ<-recode(gro$nivacad, recodes = "0='No education'; 1:3='Less than high school'; 4:5='High school'; 11:14='More than high school'; else=NA")
gro$work<-recode(gro$conact, recodes = "10:16='Work last week'; 20:34='S/R/HW'; 35='Not work last week';else=NA")
prop.table(table(gro$afromx, gro$sex), margin =2)*100
##
## Female Male
## Afromexican 8.837502 9.299519
## No afromexican 91.162498 90.700481
prop.table(table(gro$afromx, gro$educ), margin =2)*100
##
## High school Less than high school More than high school
## Afromexican 10.392308 8.939774 10.504109
## No afromexican 89.607692 91.060226 89.495891
##
## No education
## Afromexican 8.083562
## No afromexican 91.916438
prop.table(table(gro$afromx, gro$work), margin =2)*100
##
## Not work last week S/R/HW Work last week
## Afromexican 7.895706 8.386778 10.045108
## No afromexican 92.104294 91.613222 89.954892
chisq.test(table(gro$afromx, gro$sex))
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(gro$afromx, gro$sex)
## X-squared = 41.906, df = 1, p-value = 9.575e-11
chisq.test(table(gro$afromx, gro$educ))
##
## Pearson's Chi-squared test
##
## data: table(gro$afromx, gro$educ)
## X-squared = 463.44, df = 3, p-value < 2.2e-16
chisq.test(table(gro$afromx, gro$work))
##
## Pearson's Chi-squared test
##
## data: table(gro$afromx, gro$work)
## X-squared = 628.4, df = 2, p-value < 2.2e-16
options(survey.lonely.psu = "adjust")
des<-svydesign(ids = ~gro$upm, strata = ~gro$estrato , weights = ~gro$factor, data = gro[is.na(gro$factor)==F,])
Simple weighted analysis
t1<-wtd.table(gro$afromx, gro$sex, weights = gro$factor)
t1
## Female Male
## Afromexican 89821 81070
## No afromexican 1053439 913935
t1w<-prop.table(wtd.table(gro$afromx, gro$sex, weights = gro$factor), margin = 2)*100
t2<-wtd.table(gro$afromx, gro$educ, weights = gro$factor)
t2
## High school Less than high school More than high school
## Afromexican 30355 87538 23401
## No afromexican 327994 1035159 235730
## No education
## Afromexican 23354
## No afromexican 294855
t2w<-prop.table(wtd.table(gro$afromx, gro$educ, weights = gro$factor), margin = 2)*100
t3<-wtd.table(gro$afromx, gro$work, weights = gro$factor)
t3
## Not work last week S/R/HW Work last week
## Afromexican 13215 65578 91605
## No afromexican 197913 788166 975801
t3w<-prop.table(wtd.table(gro$afromx, gro$work, weights = gro$factor), margin = 2)*100
Using survey design
t1svy<-svytable(~gro$afromx+gro$sex, design=des)
t1svy
## gro$sex
## gro$afromx Female Male
## Afromexican 89821 81070
## No afromexican 1053439 913935
tsvy1<-prop.table(svytable(~gro$afromx+gro$sex, design=des), margin = 2)*100
t2svy<-svytable(~gro$afromx+gro$educ, design=des)
t2svy
## gro$educ
## gro$afromx High school Less than high school More than high school
## Afromexican 30355 87538 23401
## No afromexican 327994 1035159 235730
## gro$educ
## gro$afromx No education
## Afromexican 23354
## No afromexican 294855
tsvy2<-prop.table(svytable(~gro$afromx+gro$educ, design=des), margin = 2)*100
t3svy<-svytable(~gro$afromx+gro$work, design=des)
t3svy
## gro$work
## gro$afromx Not work last week S/R/HW Work last week
## Afromexican 13215 65578 91605
## No afromexican 197913 788166 975801
tsvy3<-prop.table(svytable(~gro$afromx+gro$work, design=des), margin = 2)*100
Comparative statistics
stargazer(t1w, tsvy1, style="default", type="text", title="Comparative between weights and survey design")
##
## Comparative between weights and survey design
## ==============================
## Female Male NA
## ------------------------------
## 1 Afromexican Female 7.857
## 2 No afromexican Female 92.143
## 3 Afromexican Male 8.148
## 4 No afromexican Male 91.852
## ------------------------------
##
## Comparative between weights and survey design
## ==============================
## Female Male NA
## ------------------------------
## 1 Afromexican Female 7.857
## 2 No afromexican Female 92.143
## 3 Afromexican Male 8.148
## 4 No afromexican Male 91.852
## ------------------------------
stargazer(t3w, tsvy3, style="default", type="text", title="Comparative between weights and survey design")
##
## Comparative between weights and survey design
## ======================================================
## Not work last week S/R/HW Work last week
## ------------------------------------------------------
## 1 Afromexican Not work last week 6.259
## 2 No afromexican Not work last week 93.741
## 3 Afromexican S/R/HW 7.681
## 4 No afromexican S/R/HW 92.319
## 5 Afromexican Work last week 8.582
## 6 No afromexican Work last week 91.418
## ------------------------------------------------------
##
## Comparative between weights and survey design
## ======================================================
## Not work last week S/R/HW Work last week
## ------------------------------------------------------
## 1 Afromexican Not work last week 6.259
## 2 No afromexican Not work last week 93.741
## 3 Afromexican S/R/HW 7.681
## 4 No afromexican S/R/HW 92.319
## 5 Afromexican Work last week 8.582
## 6 No afromexican Work last week 91.418
## ------------------------------------------------------