-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogisticRegressionExample.R
More file actions
49 lines (40 loc) · 1.47 KB
/
Copy pathLogisticRegressionExample.R
File metadata and controls
49 lines (40 loc) · 1.47 KB
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
# Logistic Regression Example in R
# Import the data
file = "DataAnalysisProject_11_11_2014.csv"
getwd()
setwd("xx")
getwd()
logRegData = read.csv(file)
age = logRegData$age
death = logRegData$death
cursmoke = logRegData$cursmoke
bmicat = logRegData$bmicat
is.factor(bmicat)
# Two ways to visually see the dataset (commented out currently)
str(logRegData)
logRegData
# Summmary of data, including number of missing values
summary(logRegData)
# Logistic regression for death as a function of age
# Note: AIC can be used to compare different (non-nested) models
glm.out = glm(death ~ age,family=binomial(logit), data=logRegData)
# Aspects of the regression model one can look at (fitted values, coefficients, residuals, ANOVA, etc.)
summary(glm.out)
glm.out$coef
glm.out$fitted
glm.out$resid
glm.out$effects
anova(glm.out)
# Interpretation of model: OR = 1.12 for a 1 year increase in age, 3.03 for a 10 year increase in age
# In this dataset, people who are 61 years old have 1.12 times the odds of death compared to that of 60 year olds
exp(0.1107748)
exp(10*0.1107748)
# Plot data and logistic regression on graph
plot(death ~ age, data=logRegData)
lines(age, glm.out$fitted, type = "l", col="red")
title(main="Death vs. Age Data with Fitted Logistic Regression Line")
# If one wanted to have multiple covariates
# NOTE- all variables are currently considered continuous
glm.out = glm(death ~ cursmoke * bmicat * age, family=binomial(logit), data=logRegData)
summary(glm.out)
plot(glm.out$fitted)