-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4. Ordinal Logistic Regression.Rmd
More file actions
97 lines (70 loc) · 2.36 KB
/
Copy path4. Ordinal Logistic Regression.Rmd
File metadata and controls
97 lines (70 loc) · 2.36 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
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
---
title: "MESA8668 - Ordinal Logistic Regression"
author: "Gulsah Gurkan"
output:
pdf_document:
fontsize: 12pt
geometry: margin = 0.8in
header-includes:
- \setlength\parindent{24pt}
- \usepackage{placeins}
- \usepackage{setspace}
- \usepackage{chngcntr}
- \usepackage{array}
- \usepackage{graphicx}
- \usepackage{caption}
- \counterwithin{figure}{section}
- \counterwithin{table}{section}
---
```{r, setup, include=FALSE}
knitr::opts_chunk$set(
echo=FALSE, message=FALSE, warning=FALSE, comment=NA,
fig.height=8, fig.width=8
)
options(max.print = 2000, tibble.print_max = 100)
```
```{r results='hide'}
#empty environment.
rm(list = ls())
# Set directory.
setwd("...")
```
```{r}
# Load data file "space".
library(foreign)
data_space <- read.spss("space.sav", use.value.labels=F, to.data.frame=TRUE)
# Variables in the dataset.
names(data_space)
# Make sure to format categorical variables.
data_space$space <- as.factor(data_space$space)
data_space$male <- as.factor(data_space$male)
data_space$income3 <- as.factor(data_space$income3)
data_space$prestige3 <- as.factor(data_space$prestige3)
data_space$politics <- as.factor(data_space$politics)
# Step 1. Fit ordinal logistic regression model. space ~ male + prestige.
# First, set the reference groups for the categorical variables.
# function relevel() helps defining the reference group as shown below.
data_space$space <- relevel(data_space$space, ref = "1") # reference category:"too little"
data_space$male <- relevel(data_space$male, ref = "1") # reference group: "male"
# Percentages of the response variable by gender.
prop.table( table(data_space$space, data_space$male) )
# package "ordinal" in R is commonly used for ordinal logistic regression models.
#install.packages("ordinal")
library(ordinal)
# Run the model: space ~ male + prestige.
OLR_mod1 <- clm(space ~ male + prestige, data = data_space)
summary(OLR_mod1) # output for the model.
# Model fit:
# 1. Wald test.
# We can use 'AER' package in R.
#install.packages("AER")
library(AER)
coeftest(OLR_mod1) # Wald test output for the estimates.
exp( coef(OLR_mod1) ) # Odds-ratios
confint(OLR_mod1) # provides confidence intervals for the coefficients.
# 2. AIC and BIC measures.
# First, create an intercept-only (null) model.
OLR_null <- clm(space ~ 1, data = data_space)
AIC(OLR_null); AIC(OLR_mod1) # AICs.
BIC(OLR_null); BIC(OLR_mod1) # BICs.
```