forked from tbruefach/CAnD3-Data-Activity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rachel_CreateDescData&RegressionTables.Rmd
166 lines (122 loc) · 5.27 KB
/
Rachel_CreateDescData&RegressionTables.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title: 'Rachel's Replication: Create Desc data and Regression tables'
author: "Rachel Ganly"
date: "9/28/2021"
output: html_document
---
Load packages
```{r}
#1. Load required packages for analyses
# Clear dataspace
rm(list = ls())
library(tidyverse)
library(haven)
library(skimr)
library(naniar)
library(Hmisc)
library(sjlabelled)
library(gt)
library(gtsummary)
# Rachel's comments: Found these packages in the Readme list on GitHub
library(vtable) #Rachel's comment: added package for descriptive stats
```
Create Table 1 (Rachel's version)
```{r}
#The sample is now ready for producing descriptive and inferential statistics. Run
# **"Table 1.R"** to create a descriptive table. This table provides descriptive statistics
# across respondents' sex.
# Load file
load("GSS_NoMissing_Rachel.csv")
descriptive_data=gss_rachel%>%select(srh, mental_srh, sex, education, age, marital_status, family_income, number_children, visible_minority)
st(descriptive_data, group="sex",title="Summary Statistics", digits=2, summ=c('notNA(x)','mean(x)'), summ.names = c("Number of Individuals", "Mean"))
```
Create Table 1 (Version using Tyler's code)
```{r}
tbl_1 <- gss_rachel %>%
tbl_summary(include = c(srh,
mental_srh,
education,
family_income,
age,
marital_status,
number_children,
visible_minority),
type = list(family_income ~ 'continuous',
number_children ~ 'continuous'),
by = sex,
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_dichotomous() ~ "0.{p}",
all_categorical() ~ "0.{p}"),
digits = list(all_continuous() ~ 2,
all_categorical() ~ 0)) %>%
modify_header(update = list(label ~ '**Variables**',
stat_1 ~ '**Male ({n})**',
stat_2 ~ '**Female({n})**')) %>%
modify_footnote(update = c(stat_1, stat_2) ~
'Mean and standard deviations provided for continuous
variables. Proportions provided for categorical variables') %>%
modify_caption('**TABLE 1. DESCRIPTIVE STATISTICS (N = 19755)**') %>%
bold_labels()
# Prints Table 1 as HTML
tbl_1 %>% as_gt()
```
Create Table (Version using Tyler's code)
```{r}
#Finally, you should run **"Table 2.R"** to create a table featuring two multivariable regressions.
# Rachel's comment: I could not tell how to do this without looking at the code as there were no instructions or comments about which variables to use in the regression or which standard errors to use
# CREATING TABLE 2: OLS REGRESSIONS OF SRH AND SRMH
# Linear Model of Self-Rated Health
# y ~ x1 + x2 + x3 ... , data = )
# Creating SRH Table based on Linear Model
# Linear Model of Self-Rated Mental Health
# y ~ x1 + x2 + x3 ... , data = )
# Creating SRMH Table based on Linear Model
# Merging SRH and SRMH Tables to Create and Store Table 2: tbl_2
# Prints Table 2 in HTML
# Linear Model of Self-Rated Health
# y ~ x1 + x2 + x3 ... , data = )
ols_srh <- lm(srh ~ education * sex +
age +
marital_status +
family_income +
number_children +
visible_minority,
data = gss_rachel,
weights = individual_survey_weight)
# Creating SRH Table based on Linear Model
model_srh <-
tbl_regression(ols_srh) %>%
modify_header(update = list(label ~ '**Variables**',
estimate ~ '***b***',
std.error ~ '***se***',
p.value ~ '**p-value**')) %>%
add_significance_stars()
# Linear Model of Self-Rated Mental Health
# y ~ x1 + x2 + x3 ... , data = )
ols_srmh <- lm(mental_srh ~ education * sex +
age +
marital_status +
family_income +
number_children +
visible_minority,
data = gss_rachel,
weights = individual_survey_weight)
# Creating SRMH Table based on Linear Model
model_srmh <-
tbl_regression(ols_srmh) %>%
modify_header(update = list(label ~ '**Variables**',
estimate ~ '***b***',
std.error ~ '***se***',
p.value ~ '**p-value**')) %>%
add_significance_stars()
# Merging SRH and SRMH Tables to Create and Store Table 2: tbl_2
tbl_2 <-
tbl_merge(list(model_srh, model_srmh),
tab_spanner = c('**Self-Rated Health**',
'**Self-Rated Mental Health**')) %>%
modify_caption('**TABLE 2. SEX MODERATES THE HEALTH-BENEFITS OF
POSTSECONDARY EDUCATIONAL ATTAINMENT, NET OF CONTROLS (N = 19755)**') %>%
modify_footnote(update = c('p.value_1', 'p.value_2') ~ 'Significant p-values in bold.')
# Prints Table 2 in HTML
tbl_2 %>% as_gt()
```