-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter 3 Standalone Code.R
228 lines (162 loc) · 7.21 KB
/
Chapter 3 Standalone 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
###--- Chapter 3 Code ---###
### Install and load the packages:
# TAM:
citation("TAM")
install.packages("TAM")
library("TAM")
# WrightMap:
citation("WrightMap")
install.packages("WrightMap")
library("WrightMap")
# eRm
citation("eRm")
install.packages("eRm")
library("eRm")
# psych
citation("psych")
install.packages("psych")
library("psych")
### Import the data:
#* Note: Update your working directory or include the complete file path to apply the read.csv() function
transreas <- read.csv("transreas.csv")
### Run the Dichotomous Rasch Model
# Remove the Student ID and Grade level variables:
transreas.responses <- subset(transreas, select = -c(Student, Grade))
# Check descriptive statistics:
summary(transreas.responses)
# Run the model
dichot.transreas <- RM(transreas.responses)
# Request a summary of the model results
summary(dichot.transreas)
### Evaluate Unidimensionality
# Calculate person parameters
student.locations <- person.parameter(dichot.transreas)
# Calculate the model-predicted probabilities
model.prob <- pmat(student.locations)
# Calculate residuals using a modified response matrix that does not include the students with extreme scores
responses.without.extremes <- student.locations$X.ex
# Calculate residuals as the difference between the observed responses and model-predictions
resids <- responses.without.extremes - model.prob
### calculate the proportion of variance in the responses
## Variance of the observations: VO
observations.vector <- as.vector(responses.without.extremes)
VO <- var(observations.vector)
## Variance of the residuals: VR
residuals.vector <- as.vector(resids)
VR <- var(residuals.vector)
## Raw variance explained by Rasch measures: (VO - VR)/VO
(VO - VR)/VO
# Express the result as a percent:
((VO - VR)/VO) * 100
### Principal components analysis of standardized residual correlations
## Conduct a PCA of Standardized Residual Correlations
# Obtain a matrix with the standardized residuals from the model
item.fit <- itemfit(student.locations)
# Extract the standardized residuals
std.resids <- item.fit$st.res
# Conduct the PCA on the standardized residuals object
pca <- pca(std.resids, nfactors = ncol(transreas.responses), rotate = "none")
# Save the first five contrasts in a vector
contrasts <- c(pca$values[1], pca$values[2], pca$values[3], pca$values[4], pca$values[5])
# Plot the values using a simple graphical display
plot(contrasts, ylab = "Eigenvalues for Contrasts", xlab = "Contrast Number", main = "Contrasts from PCA of Standardized Residual Correlations")
### Summaries of Residuals: Infit & Outfit
# Examine the item.fit
item.fit
# Calculate numeric person fit statistics
person.fit <- personfit(student.locations)
# Explore the infit and outfit statistics
summary(person.fit$p.infitMSQ)
summary(person.fit$p.outfitMSQ)
### Graphical Displays for Evaluating Model-Data Fit
# Before constructing the plots, find the maximum and minimum values of the standardized residuals to set limits for the axes:
max.resid <- ceiling(max(std.resids))
min.resid <- ceiling(min(std.resids))
# The code below will produce standardized residual plots for each of the items:
for(item.number in 1:ncol(std.resids)){
plot(std.resids[, item.number], ylim = c(min.resid, max.resid),
main = paste("Standardized Residuals for Item ", item.number, sep = ""),
ylab = "Standardized Residual", xlab = "Person Index")
abline(h = 0, col = "blue")
abline(h=2, lty = 2, col = "red")
abline(h=-2, lty = 2, col = "red")
legend("topright", c("Std. Residual", "Observed = Expected", "+/- 2 SD"), pch = c(1, NA, NA),
lty = c(NA, 1, 2),
col = c("black", "blue", "red"), cex = .8)
}
## Empirical item response functions
# Create plots for all of the items in our data
for(item.number in 1:ncol(std.resids)){
plotICC(dichot.transreas, item.subset = item.number, empICC = list("raw"), empCI = list())
}
### Reliability Indices in Rasch Measurement
# Calculate the person separation reliability statistic
summary(SepRel(student.locations))
### Conduct Model-Data Fit Analyses using the TAM Package with Joint Maximum Likelihood Estimation
# Run the Rasch model
TAM_jmle_dichot.transreas <- tam.jml(transreas.responses)
## Evaluate unidimensionality
## Isolate the response matrix used in estimation:
resp <- TAM_jmle_dichot.transreas$resp
## Find the expected response probabilities based on the model:
resids <- IRT.residuals(TAM_jmle_dichot.transreas)
exp <- resids$X_exp
## Calculate raw (unstandardized) residuals:
resids.raw <- as.matrix(resp - exp)
## Calculate the variance in observations due to Rasch-model-estimated locations:
# Variance of the observations: VO
observations.vector <- as.vector(as.matrix(resp))
VO <- var(observations.vector)
# Variance of the residuals: VR
residuals.vector <- as.vector(resids.raw)
VR <- var(residuals.vector)
# Raw variance explained by Rasch measures: (VO - VR)/VO
(VO - VR)/VO
# Express the result as a percent:
((VO - VR)/VO) * 100
# Principal components analysis of standardized residual correlations
# Conduct the PCA on the standardized residuals object
pca <- pca(resids$stand_residuals, nfactors = ncol(transreas.responses), rotate = "none")
# Save the first five contrasts in a vector
contrasts <- c(pca$values[1], pca$values[2], pca$values[3], pca$values[4], pca$values[5])
# Plot the values using a simple graphical display
plot(contrasts, ylab = "Eigenvalues for Contrasts", xlab = "Contrast Number", main = "Contrasts from PCA of Standardized Residual Correlations")
# Numeric fit statistics
# Calculate numeric item and person fit statistics:
fit.results <- tam.fit(TAM_jmle_dichot.transreas)
item.fit <- fit.results$fit.item
person.fit <- fit.results$fit.person
# Generate plots of standardized residuals for individual items
std.resids <- resids$stand_residuals
# Find the maximum and minimum values of the standardized residuals to set limits for the axes:
max.resid <- ceiling(max(std.resids))
min.resid <- ceiling(min(std.resids))
# Produce standardized residual plots for each of the items
for(item.number in 1:ncol(std.resids)){
plot(std.resids[, item.number], ylim = c(min.resid, max.resid),
main = paste("Standardized Residuals for Item ", item.number, sep = ""),
ylab = "Standardized Residual", xlab = "Person Index")
abline(h = 0, col = "blue")
abline(h=2, lty = 2, col = "red")
abline(h=-2, lty = 2, col = "red")
legend("topright", c("Std. Residual", "Observed = Expected", "+/- 2 SD"), pch = c(1, NA, NA),
lty = c(NA, 1, 2),
col = c("black", "blue", "red"), cex = .8)
}
# Reliability of separation statistics
## Person separation reliability:
TAM_jmle_dichot.transreas$WLEreliability
## Item separation reliability:
# Get Item scores
ItemScores <- colSums(transreas.responses)
# Get Item SD
ItemSD <- apply(transreas.responses,2,sd)
# Calculate the se of the Item
ItemSE <- ItemSD/sqrt(length(ItemSD))
# compute the Observed Variance (also known as Total Person Variability or Squared Standard Deviation)
SSD.ItemScores <- var(ItemScores)
# compute the Mean Square Measurement error (also known as Model Error variance)
Item.MSE <- sum((ItemSE)^2) / length(ItemSE)
# compute the Item Separation Reliability
item.separation.reliability <- (SSD.ItemScores-Item.MSE) / SSD.ItemScores
item.separation.reliability