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(questionr)
library(tableone)
## Warning: package 'tableone' was built under R version 4.0.4
library(boot)
##
## Attaching package: 'boot'
## The following object is masked from 'package:survival':
##
## aml
## The following object is masked from 'package:car':
##
## logit
library(table1)
## Warning: package 'table1' was built under R version 4.0.4
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
library(haven)
NSDUH_2019 <- read_sav("NSDUH_2019.SAV")
View(NSDUH_2019)
nams<-names(NSDUH_2019)
head(nams, n=10)
## [1] "QUESTID2" "FILEDATE" "CIGEVER" "CIGOFRSM" "CIGWILYR" "CIGTRY"
## [7] "CIGYFU" "CIGMFU" "CIGREC" "CIG30USE"
newnames<-tolower(gsub(pattern = "_",replacement = "",x = nams))
names(NSDUH_2019)<-newnames
NSDUH_2019$attempt_suicide<-Recode(NSDUH_2019$adwrsatp, recodes="1=1; 2=0;else=NA", as.factor=T)
summary(NSDUH_2019$attempt_suicide, na.rm = TRUE)
## 0 1 NA's
## 2650 887 52599
## Main variable hopeless last thirty days
NSDUH_2019$hopelesslst30days<-Recode(NSDUH_2019$dsthop30,
recodes="1:2=3;3:4=2;5=1; else=NA")
NSDUH_2019$fltnervouslst30days<-Recode(NSDUH_2019$dstnrv30,
recodes="1:2=3;3:4=2;5=1; else=NA")
NSDUH_2019$fltrestlesslst30days<-Recode(NSDUH_2019$dstrst30,
recodes="1:2=3;3:4=2;5=1; else=NA")
NSDUH_2019$fltsadlst30days<-Recode(NSDUH_2019$dstchr30,
recodes="1:2=3;3:4=2;5=1; else=NA")
NSDUH_2019$effortlst30days<-Recode(NSDUH_2019$dsteff30,
recodes="1:2=3;3:4=2;5=1; else=NA")
NSDUH_2019$fltdwnlst30days<-Recode(NSDUH_2019$dstngd30,
recodes="1:2=3;3:4=2;5=1; else=NA")
## marital status
NSDUH_2019$marst<-Recode(NSDUH_2019$irmarit, recodes="1='married'; 2='divorced'; 3='widowed'; 4='separated'; else=NA", as.factor=T)
## education recodes
NSDUH_2019$educ<-Recode(NSDUH_2019$ireduhighst2, recodes="1:7='LssThnHgh'; 8='Highschool'; 9='Some_College'; 10='Associates'; 11='College';else=NA", as.factor=T)
## sexuality recodes
NSDUH_2019$sexuality<-Recode(NSDUH_2019$sexident, recodes="1='Heterosexual'; 2='Les/Gay'; 3='Bisexual';else=NA", as.factor=T)
## gender recodes
NSDUH_2019$male<-as.factor(ifelse(NSDUH_2019$irsex==1, "Male", "Female"))
## Race recoded items
NSDUH_2019$Black<-Recode(NSDUH_2019$newrace2, recodes="2=1; 9=NA; else=0")
NSDUH_2019$White<-Recode(NSDUH_2019$newrace2, recodes="1=1; 9=NA; else=0")
NSDUH_2019$Other<-Recode(NSDUH_2019$newrace2, recodes="3:4=1; 9=NA; else=0")
NSDUH_2019$Multi_Racial<-Recode(NSDUH_2019$newrace2, recodes="6=1; 9=NA; else=0")
NSDUH_2019$Asian<-Recode(NSDUH_2019$newrace2, recodes="5=1; 9=NA; else=0")
NSDUH_2019$Hispanic<-Recode(NSDUH_2019$newrace2, recodes="7=1; 9=NA; else=0")
NSDUH_2019$race_eth<-Recode(NSDUH_2019$newrace2,
recodes="1='White'; 2='Black'; 3='Other'; 4='Asian'; 5='Multi_Racial'; 6='Hispanic'; else=NA",
as.factor = T)
NSDUH_2019$lst_alc_use2<-Recode(NSDUH_2019$iralcrc, recodes="1='last30days'; 2='12>1month'; 3='>12months'; else=NA", as.factor=T)
NSDUH_2019$dep_year2<-Recode(NSDUH_2019$amdeyr, recodes="1=1; 2=0;else=NA", as.factor=T)
NSDUH_2019$age_cat<-Recode(NSDUH_2019$age2, recodes="7:8='18-19'; 9:10='20-21'; 11='22-23'; 12='24-25'; 13='26-29'; 14='30-34'; 15='35-49'; 16='50-64'; 17='65+'; else=NA", as.factor=T)
NSDUH_2019$new_income<-Recode(NSDUH_2019$income, recodes="1='>20,000'; 2='20,000-49,999'; 3='50,000-74,999'; 4='75,000+'; else=NA", as.factor=T)
sub<-NSDUH_2019%>%
select(attempt_suicide, male, age_cat, race_eth, marst, educ, White, Black, Hispanic, Other, Asian, Multi_Racial, sexuality, lst_alc_use2, dep_year2, age2, new_income, analwtc, vestr) %>%
filter( complete.cases( . ))
#First we tell R our survey design
options(survey.lonely.psu = "adjust")
des<-svydesign(ids= ~1,
strata= ~vestr,
weights= ~analwtc
, data = sub )
t1<-CreateTableOne(vars = c("educ", "marst", "male", "sexuality", "age2", "income", "race_eth", "age_cat", "lst_alc_use2", "dep_year2", "new_income"), strata = "attempt_suicide", test = T, data = sub)
## Warning in ModuleReturnVarsExist(vars, data): The data frame does not have:
## income Dropped
#st1<-print(st1, format="p")
print(t1, format="p")
## Stratified by attempt_suicide
## 0 1 p test
## n 2047 708
## educ (%) <0.001
## Associates 10.2 9.5
## College 31.0 17.2
## Highschool 22.1 27.3
## LssThnHgh 5.4 11.3
## Some_College 31.4 34.7
## marst (%) 0.278
## divorced 1.4 1.7
## married 27.3 26.8
## separated 60.5 58.1
## widowed 10.8 13.4
## male = Male (%) 40.0 33.3 0.002
## sexuality (%) <0.001
## Bisexual 18.1 26.6
## Heterosexual 77.5 69.1
## Les/Gay 4.4 4.4
## age2 (mean (SD)) 12.63 (2.60) 12.38 (2.64) 0.032
## race_eth (%) <0.001
## Asian 0.2 0.6
## Black 7.2 7.8
## Hispanic 5.5 9.2
## Multi_Racial 3.8 3.5
## Other 1.2 3.1
## White 82.2 75.8
## age_cat (%) 0.161
## 18-19 9.2 10.7
## 20-21 10.9 13.1
## 22-23 13.1 11.6
## 24-25 12.2 13.7
## 26-29 11.3 11.6
## 30-34 10.3 11.6
## 35-49 22.7 18.5
## 50-64 7.0 5.8
## 65+ 3.2 3.4
## lst_alc_use2 (%) 0.126
## >12months 12.2 14.7
## 12>1month 19.7 21.0
## last30days 68.1 64.3
## dep_year2 = 1 (%) 60.0 61.2 0.615
## new_income (%) <0.001
## >20,000 20.2 25.6
## 20,000-49,999 29.7 33.8
## 50,000-74,999 17.1 15.4
## 75,000+ 32.9 25.3
table1(~ male + age_cat + marst + educ + sexuality + race_eth + lst_alc_use2 + dep_year2 + new_income | attempt_suicide, data=sub, overall="Total")
| 0 (N=2047) |
1 (N=708) |
Total (N=2755) |
|
|---|---|---|---|
| male | |||
| Female | 1228 (60.0%) | 472 (66.7%) | 1700 (61.7%) |
| Male | 819 (40.0%) | 236 (33.3%) | 1055 (38.3%) |
| age_cat | |||
| 18-19 | 188 (9.2%) | 76 (10.7%) | 264 (9.6%) |
| 20-21 | 224 (10.9%) | 93 (13.1%) | 317 (11.5%) |
| 22-23 | 269 (13.1%) | 82 (11.6%) | 351 (12.7%) |
| 24-25 | 250 (12.2%) | 97 (13.7%) | 347 (12.6%) |
| 26-29 | 232 (11.3%) | 82 (11.6%) | 314 (11.4%) |
| 30-34 | 211 (10.3%) | 82 (11.6%) | 293 (10.6%) |
| 35-49 | 465 (22.7%) | 131 (18.5%) | 596 (21.6%) |
| 50-64 | 143 (7.0%) | 41 (5.8%) | 184 (6.7%) |
| 65+ | 65 (3.2%) | 24 (3.4%) | 89 (3.2%) |
| marst | |||
| divorced | 29 (1.4%) | 12 (1.7%) | 41 (1.5%) |
| married | 558 (27.3%) | 190 (26.8%) | 748 (27.2%) |
| separated | 1238 (60.5%) | 411 (58.1%) | 1649 (59.9%) |
| widowed | 222 (10.8%) | 95 (13.4%) | 317 (11.5%) |
| educ | |||
| Associates | 208 (10.2%) | 67 (9.5%) | 275 (10.0%) |
| College | 634 (31.0%) | 122 (17.2%) | 756 (27.4%) |
| Highschool | 452 (22.1%) | 193 (27.3%) | 645 (23.4%) |
| LssThnHgh | 110 (5.4%) | 80 (11.3%) | 190 (6.9%) |
| Some_College | 643 (31.4%) | 246 (34.7%) | 889 (32.3%) |
| sexuality | |||
| Bisexual | 371 (18.1%) | 188 (26.6%) | 559 (20.3%) |
| Heterosexual | 1586 (77.5%) | 489 (69.1%) | 2075 (75.3%) |
| Les/Gay | 90 (4.4%) | 31 (4.4%) | 121 (4.4%) |
| race_eth | |||
| Asian | 4 (0.2%) | 4 (0.6%) | 8 (0.3%) |
| Black | 147 (7.2%) | 55 (7.8%) | 202 (7.3%) |
| Hispanic | 112 (5.5%) | 65 (9.2%) | 177 (6.4%) |
| Multi_Racial | 77 (3.8%) | 25 (3.5%) | 102 (3.7%) |
| Other | 24 (1.2%) | 22 (3.1%) | 46 (1.7%) |
| White | 1683 (82.2%) | 537 (75.8%) | 2220 (80.6%) |
| lst_alc_use2 | |||
| >12months | 250 (12.2%) | 104 (14.7%) | 354 (12.8%) |
| 12>1month | 403 (19.7%) | 149 (21.0%) | 552 (20.0%) |
| last30days | 1394 (68.1%) | 455 (64.3%) | 1849 (67.1%) |
| dep_year2 | |||
| 0 | 819 (40.0%) | 275 (38.8%) | 1094 (39.7%) |
| 1 | 1228 (60.0%) | 433 (61.2%) | 1661 (60.3%) |
| new_income | |||
| >20,000 | 414 (20.2%) | 181 (25.6%) | 595 (21.6%) |
| 20,000-49,999 | 608 (29.7%) | 239 (33.8%) | 847 (30.7%) |
| 50,000-74,999 | 351 (17.1%) | 109 (15.4%) | 460 (16.7%) |
| 75,000+ | 674 (32.9%) | 179 (25.3%) | 853 (31.0%) |
library(ggplot2)
attach(sub)
# stacked bar chart
ggplot(sub,
aes(x = race_eth,
fill = attempt_suicide)) +
geom_bar(position = "stack")
ggplot(sub,
aes(x = race_eth,
fill = attempt_suicide)) +
geom_bar(position = "dodge")
library(scales)
ggplot(sub,
aes(x = factor(race_eth,
levels = c("White", "Hispanic",
"Black", "Asian",
"Other", "Multi_Racial")),
fill = factor(attempt_suicide,
levels = c("1", "0"),
labels = c("Yes",
"No")))) +
geom_bar(position = "fill") +
scale_y_continuous(breaks = seq(0, 1, .2),
label = percent) +
scale_fill_brewer(palette = "Set2") +
labs(y = "Percent",
fill = "Suicide Attempts By Sex",
x = "Race/Ethnicity",
title = "Suicide Attempts By Race/Ethnicity") +
theme_minimal()
library(scales)
ggplot(sub,
aes(x = factor(male,
levels = c("Male", "Female")),
fill = factor(attempt_suicide,
levels = c("1", "0"),
labels = c("Yes",
"No")))) +
geom_bar(position = "fill") +
scale_y_continuous(breaks = seq(0, 1, .2),
label = percent) +
scale_fill_brewer(palette = "Set2") +
labs(y = "Percent",
fill = "Made A Suicide Attempt",
x = "Sex",
title = "Suicide Attempts By Sex") +
theme_minimal()