-
Notifications
You must be signed in to change notification settings - Fork 2
/
20211210-logistic-regression-tutorial-code.R
125 lines (101 loc) · 4.52 KB
/
20211210-logistic-regression-tutorial-code.R
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
############################################################################
# Author: Jenine K. Harris
# Date last edited: Nov 23, 2021
# Data source: mtcars built-in data
# Contact info: harrisj@wustl.edu
# URL of code: https://github.com/jenineharris/logistic-regression-tutorial
############################################################################
# before running code, install packages through the tools menu
# packages to install: tidyverse, table1, finalfit, knitr, odds.n.ends, car
# open packages using the library function
library(package = "tidyverse")
library(package = "table1")
library(package = "finalfit")
library(package = "knitr")
# using built-in cars data set, changing variable names for example
# data cleaning and management
lungCancer <- mtcars$vs
yearsSmoke <- mtcars$mpg
bmi <- mtcars$am
lungCancerData <- data.frame(lungCancer, yearsSmoke, bmi)
lungCancerData <- lungCancerData %>%
mutate(bmi = recode_factor(bmi,
'0' = "Underweight or Normal",
'1' = "Overweight or Obese")) %>%
mutate(lungCancer = recode_factor(lungCancer,
'0' = "No Lung Cancer Diagnosis",
'1' = "Yes Lung Cancer Diagnosis"))
label(lungCancerData$yearsSmoke) <- "Years of cigarette smoking"
label(lungCancerData$bmi) <- "Body mass index category"
label(lungCancerData$lungCancer) <- "Ever diagnosed with lung cancer"
# STEP 1: EXPLORATORY DATA ANALYSIS
# histogram of years smoking
yearsHisto <- lungCancerData %>%
ggplot(aes(x = yearsSmoke)) +
geom_histogram(bins = 8, color = 'white') +
theme_minimal(base_size = 14, base_family = "serif") +
labs(x = "Years of cigarette smoking", y = "Frequency")
yearsHisto
# table 1: basic table of lung cancer, years smoking, bmi
tableDesc <- lungCancerData %>%
summary_factorlist(explanatory = c('lungCancer',
'yearsSmoke',
'bmi'),
cont = "median")
table1 <- kable(tableDesc, row.names=FALSE,
col.names = c("Characteristic",
"Category",
"n (%)"),
align=c("l", "l", "r"))
table1
# table 2: crosstabs of lung cancer, years smoking, bmi
tableInf <- lungCancerData %>%
summary_factorlist(dependent = c('lungCancer'),
explanatory = c('yearsSmoke',
'bmi'),