Lab exercise 6

1. Suppose you want to create a function named my_obj that takes three inputs:
• response data frame resp

• a criterion score vector Y

• weight $$w$$, where the default weight is $$w = 0.5$$.

and returns the weighted average of the reliability $$s_ir_{iX}$$ and the item validity-to-reliability ratio $$\frac{s_ir_{iY}}{s_ir_{iX}}$$, that is,

$w\cdot s_ir_{iX}+(1-w)\frac{s_{i}r_{iY}}{s_ir_{iX}}$.

Complete the following code chunk:

my_obj <- function(resp, Y, w = 0.5){
# item difficulty
difficulty <- colMeans(resp)

# item discrimination
X <- rowSums(resp)
discrimination <- numeric(ncol(resp))
for (i in 1:ncol(resp)) {
discrimination[i] <- cor(resp[, i], X)
}

# item SD
item_sd <-

# item reliability
item_rel <-

# item validity
r_iy <- numeric(ncol(resp))
for (i in 1:ncol(resp)) {
r_iy[i] <-
}
item_val <-

# weighted average
obj <- w * item_rel + (1 - w) * (item_val / item_rel)

return(obj)
}

order(my_obj(responses, Y), decreasing = TRUE)[1:10]
1. Try different values of w (0.0, 0.1, 0.2, …, 0.8, 0.9, 1.0). For each w values, select 10 items with the largest weighted average, $$w \cdot s_ir_{iX} + (1-w) \cdot \frac{s_{i} r_{iY}}{s_i r_{iX}}$$.

hints:

• ws <- seq(0, 1, 0.1) will create a vector from 0 to 1 with 0.1 increment.
ws <- seq(0, 1, 0.1)
ws
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5
##  [7] 0.6 0.7 0.8 0.9 1.0
• Create an output object which is a $$11 \times 10$$ matrix.
output <- matrix(NA, nrow = length(ws), ncol = 10)
• Write a for loop for (i in 1:length(ws)) {}

• At each iteration in the for loop:

• set the new w as the $$i$$th element of ws

• obtain the weighted average using my_obj() function with the new weight w

• find the 10 item numbers that maximize obj

• save the 10 item numbers to the $$i$$th row of the output matrix