if (FALSE) {
#############################################################################
# EXAMPLE 1: Two items, moderator on (0,1)
#############################################################################
#*** simulate data
# functions for thresholds
th1_fun <- function(x){ -0.3*(x-2)^2 + .2 }
th2_fun <- function(x){ 0.4*(x+1)^2 - 0.6 }
zh1_fun <- function(x){ 0.3*(x-1) }
# function polychoric correlation
cor_x12 <- function(x){ 0.2+0.1*(x-0.5)+0.09*(x-0.5)^2 }
# simulate moderator
x <- stats::runif(N)
# simulate data
yast <- matrix( NA, nrow=N, ncol=2)
for (nn in 1:N){
rho12 <- cor_x12(x[nn])
Sigma <- matrix(0, 2,2)
Sigma[1,2] <- rho12
Sigma <- Sigma + t(Sigma)
diag(Sigma) <- 1
yast_nn <- MASS::mvrnorm( 1, mu=rep(0,2), Sigma=Sigma )
yast[nn,] <- yast_nn
}
y <- 0*yast
th1_x <- th1_fun(x)
th2_x <- th2_fun(x)
zh1_x <- zh1_fun(x)
y[,1] <- 1*( yast[,1] > th1_x ) + 1*( yast[,1] > th2_x )
y[,2] <- 1*( yast[,2] > zh1_x )
colnames(y) <- paste0("I",1:ncol(y))
dat <- data.frame(x=x, y)
#-- local modeling
res <- sirt::locpolycor(y, data.mod=x, moderator.grid=c(0, .25, .5, .75, 1 ), h=2,
model_thresh="lin", model_polycor="lin")
str(res)
}
Run the code above in your browser using DataLab