library(car)
## Loading required package: carData
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. 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)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v stringr 1.4.0
## v tidyr 1.1.4 v forcats 0.5.1
## v readr 2.1.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x tidyr::expand() masks Matrix::expand()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x tidyr::pack() masks Matrix::pack()
## x dplyr::recode() masks car::recode()
## x purrr::some() masks car::some()
## x tidyr::unpack() masks Matrix::unpack()
library(broom)
library(emmeans)
library(table1)
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
library(ggplot2)
library(haven)
GSS2018 <- read_sav("GSS2018.sav")
View(GSS2018)
nams<-names(GSS2018)
head(nams, n=10)
## [1] "ABANY" "ABDEFECT" "ABFELEGL" "ABHELP1" "ABHELP2" "ABHELP3"
## [7] "ABHELP4" "ABHLTH" "ABINSPAY" "ABMEDGOV1"
newnames<-tolower(gsub(pattern = "_",replacement = "",x = nams))
names(GSS2018)<-newnames
GSS2018$sup_dp<-Recode(GSS2018$cappun, recodes="1=1; 2=0;else=NA")
summary(GSS2018$sup_dp, na.rm = TRUE)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.0000 0.0000 1.0000 0.6315 1.0000 1.0000 155
GSS2018$sex <- as.numeric(GSS2018$sex)
GSS2018$geniden<-Recode (GSS2018$sex, recodes="1='male'; 2='female'; else=NA", as.factor=T)
GSS2018geniden<-relevel(GSS2018$geniden, ref='female')
summary(GSS2018$geniden, na.rm = TRUE)
## female male
## 1296 1052
GSS2018$race <- as.numeric(GSS2018$race)
GSS2018$race_eth2<-Recode(GSS2018$race, recodes="1='white'; 2='black'; 3='other'; else=NA", as.factor=T)
GSS2018race_eth2<-relevel(GSS2018$race_eth2, ref='white')
summary(GSS2018$race_eth2, na.rm = TRUE)
## black other white
## 385 270 1693
GSS2018$partyid <- as.numeric(GSS2018$partyid)
GSS2018$polaff<-Recode(GSS2018$partyid, recodes="0:2='dem'; 4:6='repub'; 3='independent'; else=NA", as.factor=T)
GSS2018polaff<-relevel(GSS2018$polaff, ref='dem')
summary(GSS2018$polaff, na.rm = TRUE)
## dem independent repub NA's
## 1038 414 786 110
GSS2018$degree <- as.numeric(GSS2018$degree)
GSS2018$educ_new<-Recode(GSS2018$degree, recodes="0='lsshgh'; 1='hghsch'; 2='smecol'; 3:4='col'; else=NA", as.factor=T)
GSS2018educ_new<-relevel(GSS2018$educ_new, ref='col')
summary(GSS2018$educ_new, na.rm = TRUE)
## col hghsch lsshgh smecol
## 712 1178 262 196
sub<-GSS2018%>%
select(sup_dp, geniden, race_eth2, polaff, educ_new, vstrat, wtssall) %>%
filter( complete.cases( . ))
table1(~ race_eth2 + geniden + educ_new + polaff | sup_dp, data=sub, overall="Total")
## Warning in table1.formula(~race_eth2 + geniden + educ_new + polaff | sup_dp, :
## Terms to the right of '|' in formula 'x' define table columns and are expected
## to be factors with meaningful labels.
|
0 (N=771) |
1 (N=1322) |
Total (N=2093) |
| race_eth2 |
|
|
|
| black |
183 (23.7%) |
159 (12.0%) |
342 (16.3%) |
| other |
102 (13.2%) |
133 (10.1%) |
235 (11.2%) |
| white |
486 (63.0%) |
1030 (77.9%) |
1516 (72.4%) |
| geniden |
|
|
|
| female |
461 (59.8%) |
674 (51.0%) |
1135 (54.2%) |
| male |
310 (40.2%) |
648 (49.0%) |
958 (45.8%) |
| educ_new |
|
|
|
| col |
297 (38.5%) |
342 (25.9%) |
639 (30.5%) |
| hghsch |
325 (42.2%) |
718 (54.3%) |
1043 (49.8%) |
| lsshgh |
97 (12.6%) |
138 (10.4%) |
235 (11.2%) |
| smecol |
52 (6.7%) |
124 (9.4%) |
176 (8.4%) |
| polaff |
|
|
|
| dem |
492 (63.8%) |
485 (36.7%) |
977 (46.7%) |
| independent |
138 (17.9%) |
226 (17.1%) |
364 (17.4%) |
| repub |
141 (18.3%) |
611 (46.2%) |
752 (35.9%) |
options(survey.lonely.psu = "adjust")
des<-svydesign(ids= ~1,
strata= ~vstrat,
weights= ~wtssall
, data = sub )
fit.logit1<-svyglm(sup_dp ~ race_eth2 + geniden + educ_new,
design = des,
family = binomial)
## Warning in eval(family$initialize): non-integer #successes in a binomial glm!
fit.logit2<-svyglm(sup_dp ~ race_eth2 + geniden + educ_new + polaff,
design = des,
family = binomial)
## Warning in eval(family$initialize): non-integer #successes in a binomial glm!
fit.logit1%>%
tidy()%>%
mutate(OR = exp(estimate),
LowerOR_Ci = exp(estimate - 1.96*std.error),
UpperOR_Ci = exp(estimate + 1.96*std.error))%>%
knitr::kable(digits = 3)
| (Intercept) |
-0.868 |
0.162 |
-5.363 |
0.000 |
0.420 |
0.306 |
0.577 |
| race_eth2other |
0.611 |
0.207 |
2.959 |
0.003 |
1.843 |
1.229 |
2.763 |
| race_eth2white |
1.021 |
0.145 |
7.031 |
0.000 |
2.776 |
2.088 |
3.689 |
| genidenmale |
0.336 |
0.109 |
3.086 |
0.002 |
1.400 |
1.131 |
1.733 |
| educ_newhghsch |
0.776 |
0.123 |
6.291 |
0.000 |
2.172 |
1.706 |
2.766 |
| educ_newlsshgh |
0.272 |
0.188 |
1.450 |
0.147 |
1.313 |
0.909 |
1.896 |
| educ_newsmecol |
0.827 |
0.223 |
3.701 |
0.000 |
2.286 |
1.476 |
3.543 |
fit.logit2%>%
tidy()%>%
mutate(OR = exp(estimate),
LowerOR_Ci = exp(estimate - 1.96*std.error),
UpperOR_Ci = exp(estimate + 1.96*std.error))%>%
knitr::kable(digits = 3)
| (Intercept) |
-1.025 |
0.171 |
-5.984 |
0.000 |
0.359 |
0.256 |
0.502 |
| race_eth2other |
0.372 |
0.216 |
1.717 |
0.086 |
1.450 |
0.949 |
2.216 |
| race_eth2white |
0.568 |
0.155 |
3.667 |
0.000 |
1.764 |
1.302 |
2.389 |
| genidenmale |
0.270 |
0.113 |
2.393 |
0.017 |
1.310 |
1.050 |
1.634 |
| educ_newhghsch |
0.739 |
0.130 |
5.693 |
0.000 |
2.094 |
1.624 |
2.701 |
| educ_newlsshgh |
0.290 |
0.196 |
1.479 |
0.139 |
1.336 |
0.910 |
1.961 |
| educ_newsmecol |
0.835 |
0.225 |
3.717 |
0.000 |
2.304 |
1.484 |
3.578 |
| polaffindependent |
0.510 |
0.146 |
3.485 |
0.001 |
1.665 |
1.250 |
2.217 |
| polaffrepub |
1.413 |
0.137 |
10.307 |
0.000 |
4.109 |
3.141 |
5.376 |
exp(coefficients(fit.logit1))
## (Intercept) race_eth2other race_eth2white genidenmale educ_newhghsch
## 0.4199079 1.8428037 2.7756429 1.3997594 2.1720459
## educ_newlsshgh educ_newsmecol
## 1.3128135 2.2864526
exp(coefficients(fit.logit2))
## (Intercept) race_eth2other race_eth2white genidenmale
## 0.3586729 1.4499234 1.7641018 1.3100384
## educ_newhghsch educ_newlsshgh educ_newsmecol polaffindependent
## 2.0942691 1.3357832 2.3040053 1.6646322
## polaffrepub
## 4.1091071
rg<-ref_grid(fit.logit2)
marg_logit<-emmeans(object = rg,
specs = c( "geniden", "polaff"),
type="response" ,
data=sub)
knitr::kable(marg_logit, digits = 4)
| female |
dem |
0.4387 |
0.0274 |
Inf |
0.3859 |
0.4929 |
| male |
dem |
0.5059 |
0.0292 |
Inf |
0.4488 |
0.5628 |
| female |
independent |
0.5654 |
0.0357 |
Inf |
0.4946 |
0.6337 |
| male |
independent |
0.6302 |
0.0347 |
Inf |
0.5601 |
0.6953 |
| female |
repub |
0.7626 |
0.0266 |
Inf |
0.7066 |
0.8107 |
| male |
repub |
0.8080 |
0.0229 |
Inf |
0.7592 |
0.8488 |
rg<-ref_grid(fit.logit2)
marg_logit<-emmeans(object = rg,
specs = c( "race_eth2", "polaff"),
type="response" ,
data=sub)
knitr::kable(marg_logit, digits = 4)
| black |
dem |
0.3954 |
0.0345 |
Inf |
0.3301 |
0.4647 |
| other |
dem |
0.4868 |
0.0454 |
Inf |
0.3991 |
0.5752 |
| white |
dem |
0.5357 |
0.0254 |
Inf |
0.4857 |
0.5851 |
| black |
independent |
0.5213 |
0.0454 |
Inf |
0.4326 |
0.6087 |
| other |
independent |
0.6122 |
0.0465 |
Inf |
0.5181 |
0.6987 |
| white |
independent |
0.6576 |
0.0308 |
Inf |
0.5951 |
0.7152 |
| black |
repub |
0.7288 |
0.0372 |
Inf |
0.6501 |
0.7954 |
| other |
repub |
0.7958 |
0.0325 |
Inf |
0.7249 |
0.8522 |
| white |
repub |
0.8258 |
0.0175 |
Inf |
0.7889 |
0.8575 |
rg<-ref_grid(fit.logit2)
marg_logit<-emmeans(object = rg,
specs = c( "educ_new", "polaff"),
type="response" ,
data=sub)
knitr::kable(marg_logit, digits = 4)
| col |
dem |
0.3596 |
0.0292 |
Inf |
0.3045 |
0.4185 |
| hghsch |
dem |
0.5404 |
0.0264 |
Inf |
0.4884 |
0.5916 |
| lsshgh |
dem |
0.4286 |
0.0419 |
Inf |
0.3491 |
0.5118 |
| smecol |
dem |
0.5640 |
0.0526 |
Inf |
0.4596 |
0.6630 |
| col |
independent |
0.4831 |
0.0417 |
Inf |
0.4025 |
0.5646 |
| hghsch |
independent |
0.6618 |
0.0312 |
Inf |
0.5983 |
0.7200 |
| lsshgh |
independent |
0.5552 |
0.0469 |
Inf |
0.4625 |
0.6443 |
| smecol |
independent |
0.6829 |
0.0501 |
Inf |
0.5778 |
0.7721 |
| col |
repub |
0.6976 |
0.0326 |
Inf |
0.6302 |
0.7575 |
| hghsch |
repub |
0.8285 |
0.0202 |
Inf |
0.7853 |
0.8645 |
| lsshgh |
repub |
0.7550 |
0.0365 |
Inf |
0.6766 |
0.8194 |
| smecol |
repub |
0.8417 |
0.0309 |
Inf |
0.7714 |
0.8933 |