-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrescale.r
More file actions
84 lines (72 loc) · 2.2 KB
/
rescale.r
File metadata and controls
84 lines (72 loc) · 2.2 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
#######################################################
# unidimensional IRT helper functions
# useful for unidimensional IRT comparisons
#
# Yoav Bergner, 2012
#######################################################
#######################################################
# SI2DD converts from Slope-Intercept form of logit variables
# e.g. X1 + X2*Theta
# to Difficulty/Discrimination form of variables
# e.g. Discrim*(Theta-Difficulty)
#
# the function has an optional argument depending on whether
# standard errors are included for each of the variables
#
SI2DD <- function(X1,X2,noerrs=FALSE) {
### to prevent div by zero errors
if (noerrs) {
X2[which(abs(X2) < 0.001)] <- 0.001
diffs = -X1/X2
list(diffs = diffs, discrims=X2)
} else {
X2[which(abs(X2[,1]) < 0.001),1] <- 0.001
diffs = -X1[,1]/X2[,1]
diffs.se = sqrt((X2[,1]^2*X1[,2]^2 + X1[,1]^2*X2[,2]^2)/X2[,1]^4)
list(diffs = cbind(diffs,diffs.se), discrims=X2)
}
}
#######################################################
# DD2SI converts from Difficulty/Discrimination form of logit variables
# to Slope-Intercept form of variables
#
# the function for now requires standard errors to be included
# see SI2DD
DD2SI <- function(Diff, Discrim) {
X1 = -Diff[,1]*Discrim[,1]
X1.se = sqrt(Discrim[,1]^2*Diff[,2]^2 + Diff[,1]^2*Discrim[,2]^2)
list(X1=cbind(X1,X1.se), X2=Discrim)
}
#######################################################
# RescaleSI rescales the Slope-Intercept form of the logit variables
# so that the ability distribution is unit normal
#
# similar to SI2DD, there is an option to include standard errors or not
RescaleSI <- function(Theta, X1, X2, noerrs=FALSE) {
if (noerrs) {
if (mean(X2) < 0) {
X2 = -X2
Theta = -Theta
}
sig = sd(Theta)
mu = mean(Theta)
Theta <- (Theta-mu)/sig
X1 <- X1 + mu*X2
X2 <- sig*X2
return(list(Theta=Theta, X1=X1, X2=X2))
} else {
if (mean(X2[,1]) < 0) {
X2[,1] = -X2[,1]
Theta[,1] = -Theta[,1]
}
sig = sd(Theta[,1])
mu = mean(Theta[,1])
Theta[,1] <- (Theta[,1]-mu)/sig
Theta[,2] <- Theta[,2]/sig
X1[,1] <- X1[,1] + mu*X2[,1]
X1[,2] <- sqrt(X1[,2]^2 + (mu*X2[,2])^2)
X2[,1] <- sig*X2[,1]
X2[,2] <- sig*X2[,2]
return(list(Theta=Theta, X1=X1, X2=X2))
}
}