forked from rstudio/bookdown-demo
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Chapter 2 Exercise Solution code.R
357 lines (270 loc) · 14 KB
/
Chapter 2 Exercise Solution 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
###--- Chapter 2 Exercise Solution Code ---###
### Install and load the packages:
# TAM:
citation("TAM")
install.packages("TAM")
library("TAM")
# WrightMap:
citation("WrightMap")
install.packages("WrightMap")
library("WrightMap")
### Import the mathematics assessment data:
#* Note: Update your working directory or include the complete file path to apply the read.csv() function
math_data <- read.csv("exercise_2.csv")
# Explore the data using descriptive statistics
summary(math_data)
### Run the dichotomous Rasch model:
# Remove the ID variable:
math_data.responses <- subset(math_data, select = -c(ids))
# Check descriptive statistics:
summary(math_data.responses)
# Run the Dichotomous Rasch Model
dichot.math_data <- tam(math_data.responses)
# Request a summary of the model results
summary(dichot.math_data)
# Plot the Wright Map
IRT.WrightMap(dichot.math_data, show.thr.lab=FALSE, main.title = "Mathematics Assessment Wright Map")
### Examine Item Parameters:
# View individual item parameters:
difficulty <- dichot.math_data$xsi
difficulty
# Examine descriptive statistics for item parameters:
summary(difficulty)
sd(difficulty$xsi)
sd(difficulty$se.xsi)
hist(difficulty$xsi, main = "Histogram of Item Difficulty Estimates for the Chapter 2 Mathematics Assessment Data",
xlab = "Item Difficulty Estimates in Logits")
# Examine item fit statistics:
item.fit <- tam.fit(dichot.math_data)
item.fit <- as.data.frame(item.fit$itemfit)
summary(item.fit)
### Examine Person Parameters:
achievement <- as.data.frame(tam.wle(dichot.math_data))
# Examine descriptive statistics for person parameters:
summary(achievement)
hist(achievement$theta, main = "Histogram of Person Achievement Estimates \nfor the Chapter 2 Mathematics Assessment Data",
xlab = "Person Achievement Estimates in Logits")
# Examine person fit statistics:
person.fit <- tam.personfit(dichot.math_data)
summary(person.fit)
### Summarize the results in tables
summary.table.statistics <- c("Logit Scale Location Mean",
"Logit Scale Location SD",
"Standard Error Mean",
"Standard Error SD",
"Outfit MSE Mean",
"Outfit MSE SD",
"Infit MSE Mean",
"Infit MSE SD",
"Std. Outfit Mean",
"Std. Outfit SD",
"Std. Infit Mean",
"Std. Infit SD",
"Reliability of Separation")
item.summary.results <- rbind(mean(difficulty$xsi),
sd(difficulty$xsi),
mean(difficulty$se.xsi),
sd(difficulty$se.xsi),
mean(item.fit$Outfit),
sd(item.fit$Outfit),
mean(item.fit$Infit),
sd(item.fit$Infit),
mean(item.fit$Outfit_t),
sd(item.fit$Outfit_t),
mean(item.fit$Infit_t),
sd(item.fit$Infit_t),
dichot.math_data$EAP.rel)
person.summary.results <- rbind(mean(achievement$theta),
sd(achievement$theta),
mean(achievement$error),
sd(achievement$error),
mean(person.fit$outfitPerson),
sd(person.fit$outfitPerson),
mean(person.fit$infitPerson),
sd(person.fit$infitPerson),
mean(person.fit$outfitPerson_t),
sd(person.fit$outfitPerson_t),
mean(person.fit$infitPerson_t),
sd(person.fit$infitPerson_t),
mean(achievement$WLE.rel))
# Round the values for presentation in a table:
item.summary.results_rounded <- round(item.summary.results, digits = 2)
person.summary.results_rounded <- round(person.summary.results, digits = 2)
Table1 <- cbind.data.frame(summary.table.statistics,
item.summary.results_rounded,
person.summary.results_rounded)
# add descriptive column labels:
names(Table1) <- c("Statistic", "Items", "Persons")
## Item calibration table:
# Calculate the proportion correct for each task:
TaskCorrect <- apply(math_data.responses, 2, sum)
PropCorrect <- (TaskCorrect/nrow(math_data.responses))
# Combine item calibration results in a table:
Table2 <- cbind.data.frame(item.fit$parameter,
PropCorrect,
difficulty$xsi,
difficulty$se.xsi,
item.fit$Outfit,
item.fit$Outfit_t,
item.fit$Infit,
item.fit$Infit_t)
names(Table2) <- c("Task ID", "Proportion Correct", "Item Location","Item SE","Outfit MSE","Std. Outfit", "Infit MSE","Std. Infit")
# Sort Table 2 by Item difficulty:
Table2 <- Table2[order(-Table2$`Item Location`),]
# Round the numeric values (all columns except the first one) to 2 digits:
Table2[, -1] <- round(Table2[,-1], digits = 2)
## Person calibration table:
# Calculate proportion correct for persons:
PersonPropCorrect <- achievement$PersonScores / achievement$PersonMax
# Combine person calibration results in a table:
Table3 <- cbind.data.frame(achievement$pid,
PersonPropCorrect,
achievement$theta,
achievement$error,
person.fit$outfitPerson,
person.fit$outfitPerson_t,
person.fit$infitPerson,
person.fit$infitPerson_t)
names(Table3) <- c("Person ID", "Proportion Correct", "Person Location","Person SE","Outfit MSE","Std. Outfit", "Infit MSE","Std. Infit")
# Round the numeric values (all columns except the first one) to 2 digits:
Table3[, -1] <- round(Table3[,-1], digits = 2)
### Run the Dichotomous Rasch Model with Joint Maximum Likelihood Estimation
## Estimate the Dichotomous Rasch Model using JMLE
jmle.dichot.math_data <- tam.jml(math_data.responses)
# Request a summary of the model results
summary(jmle.dichot.math_data)
# Plot a Wright Map for the JMLE results:
jmle.difficulty <- jmle.dichot.math_data$xsi
theta <- jmle.dichot.math_data$theta
wrightMap(thetas = theta, thresholds = jmle.difficulty, show.thr.lab=FALSE, main.title = "Chapter 2 Mathematics Assessment Data Wright Map: JMLE")
### JMLE Item Parameters:
# item standard errors:
jmle.item.se <- jmle.dichot.math_data$errorP
# save item parameters in a data frame object:
jmle.item.estimates <- cbind.data.frame(c(1:35),
jmle.difficulty,
jmle.item.se)
names(jmle.item.estimates) <- c("Item ID", "Item Location", "Item Location SE")
# examine item parameters in more detail:
#View(jmle.item.estimates)
summary(jmle.item.estimates)
sd(jmle.item.estimates$`Item Location`)
hist(jmle.item.estimates$`Item Location`, main = "Histogram of Item Location Estimates \nfor the Chapter 2 Mathematics Assessment Data: JMLE", xlab = "Item Location Estimates in Logits")
### JMLE Person Parameters:
# person standard errors:
jmle.person.se <- jmle.dichot.math_data$errorWLE
# save person parameters in a data frame object:
jmle.person.estimates <- cbind.data.frame(math_data$ids, theta, jmle.person.se)
names(jmle.person.estimates) <- c("Person ID", "Theta", "Theta SE")
# examine person parameters in more detail:
#View(jmle.person.estimates)
summary(jmle.person.estimates)
sd(jmle.person.estimates$Theta)
hist(jmle.person.estimates$Theta, main = "Histogram of Person Location Estimates \nfor the Chapter 2 Mathematics Assessment Data: JMLE", xlab = "Person Location Estimates in Logits")
### JMLE Fit Statistics:
jmle.fit <- tam.jml.fit(jmle.dichot.math_data)
## Examine item fit statistics:
jmle.item.fit <- as.data.frame(jmle.fit$fit.item)
jmle.item.fit
#View(jmle.item.fit)
summary(jmle.item.fit)
## Examine person fit statistics:
jmle.person.fit <- as.data.frame(jmle.fit$fit.person)
#View(jmle.person.fit)
summary(jmle.person.fit)
### Summarize the results from the JMLE dichotomous Rasch model analysis in tables
## Model summary table:
jmle.summary.table.statistics <- c("Logit Scale Location Mean",
"Logit Scale Location SD",
"Standard Error Mean",
"Standard Error SD",
"Outfit MSE Mean",
"Outfit MSE SD",
"Infit MSE Mean",
"Infit MSE SD",
"Std. Outfit Mean",
"Std. Outfit SD",
"Std. Infit Mean",
"Std. Infit SD",
"Reliability of Separation")
jmle.item.summary.results <- rbind(mean(jmle.item.estimates$`Item Location`),
sd(jmle.item.estimates$`Item Location`),
mean(jmle.item.estimates$`Item Location SE`),
sd(jmle.item.estimates$`Item Location SE`),
mean(jmle.item.fit$outfitItem),
sd(jmle.item.fit$outfitItem),
mean(jmle.item.fit$infitItem),
sd(jmle.item.fit$infitItem),
mean(jmle.item.fit$outfitItem_t),
sd(jmle.item.fit$outfitItem_t),
mean(jmle.item.fit$infitItem_t),
sd(jmle.item.fit$infitItem_t),
jmle.dichot.math_data$WLEreliability)
#*#*# Item reliability???
jmle.person.summary.results <- rbind(mean(jmle.person.estimates$Theta),
sd(jmle.person.estimates$Theta),
mean(jmle.person.estimates$`Theta SE`),
sd(jmle.person.estimates$`Theta SE`),
mean(jmle.person.fit$outfitPerson),
sd(jmle.person.fit$outfitPerson),
mean(jmle.person.fit$infitPerson),
sd(jmle.person.fit$infitPerson),
mean(jmle.person.fit$outfitPerson_t),
sd(jmle.person.fit$outfitPerson_t),
mean(jmle.person.fit$infitPerson_t),
sd(jmle.person.fit$infitPerson_t),
mean(achievement$WLE.rel))
# Round the values for presentation in a table:
jmle.item.summary.results_rounded <- round(jmle.item.summary.results, digits = 2)
jmle.person.summary.results_rounded <- round(jmle.person.summary.results, digits = 2)
jmle.Table1 <- cbind.data.frame(jmle.summary.table.statistics,
jmle.item.summary.results_rounded,
jmle.person.summary.results_rounded)
# Add descriptive column labels:
names(jmle.Table1) <- c("Statistic", "Items", "Persons")
## JMLE item calibration table:
# Calculate the proportion correct for each task:
TaskCorrect <- apply(math_data.responses, 2, sum)
PropCorrect <- (TaskCorrect/nrow(math_data.responses))
jmle.Table2 <- cbind.data.frame(jmle.item.fit$item,
PropCorrect,
jmle.difficulty,
jmle.item.se,
item.fit$Outfit,
item.fit$Outfit_t,
item.fit$Infit,
item.fit$Infit_t)
names(jmle.Table2) <- c("Task ID", "Proportion Correct", "Item Location","Item SE","Outfit MSE","Std. Outfit", "Infit MSE","Std. Infit")
# Sort the table by Item difficulty:
jmle.Table2 <- jmle.Table2[order(-jmle.Table2$`Item Location`),]
# Round the numeric values (all columns except the first one) for presentation in a table:
jmle.Table2[, -1] <- round(jmle.Table2[, -1], digits = 2)
## Person calibration table:
# calculate proportion correct for persons:
PersonPropCorrect <- achievement$PersonScores / achievement$PersonMax
jmle.Table3 <- cbind.data.frame(jmle.person.estimates$`Person ID`,
PersonPropCorrect,
jmle.person.estimates$Theta,
jmle.person.estimates$`Theta SE`,
jmle.person.fit$outfitPerson,
jmle.person.fit$outfitPerson_t,
jmle.person.fit$infitPerson,
jmle.person.fit$infitPerson_t)
names(jmle.Table3) <- c("Person ID", "Proportion Correct", "Person Location","Person SE","Outfit MSE","Std. Outfit", "Infit MSE","Std. Infit")
# Round the numeric values (all columns except the first one) for presentation in a table:
jmle.Table3[, -1] <- round(jmle.Table3[, -1], digits = 2)
### Compare the estimated parameters between JMLE and MMLE methods
## Compare item estimates:
plot(difficulty$xsi, jmle.difficulty,
pch=16,
xlab= "MMLE Estimate",
ylab= "JMLE Estimate",
main="Item Parameter Estimate Comparison")
cor(difficulty$xsi, jmle.difficulty)
## Compare person estimates:
plot(achievement$theta, jmle.person.estimates$Theta,
pch=16,
xlab= "MMLE Estimate",
ylab= "JMLE Estimate",
main="Person Parameter Estimate Comparison")
cor(achievement$theta, jmle.person.estimates$Theta)