set.seed(2010)
x <- round(rnorm(1000, 100, 15))
xscale <- 50:150
# smooth x preserving first 3 moments:
xtab <- freqtab(x, xscale = xscale)
xlog1 <- loglinear(xtab, degree = 3)
plot(xtab, y = xlog1)
lines(xtab[, 1], xlog1)
# add "teeth" and "gaps" to x:
teeth <- c(.5, rep(c(1, 1, 1, 1, .5), 20))
xt <- xtab[, 2] * teeth
cbind(xtab, xt)
xttab <- as.freqtab(cbind(xscale, xt))
xlog2 <- loglinear(xttab, degree = 3)
cbind(xscale, xt, xlog2)
# smooth xt using score functions that preserve
# the teeth structure (also 3 moments):
teeth2 <- c(1, rep(c(0, 0, 0, 0, 1), 20))
xt.fun <- cbind(xscale, xscale^2, xscale^3)
xt.fun <- cbind(xt.fun, xt.fun * teeth2)
xlog3 <- loglinear(xttab, xt.fun)
cbind(xscale, xt, xlog3)
par(mfrow = c(2, 1))
plot(xscale, xt, type = "h", ylab = "count",
main = "X teeth raw")
plot(xscale, xlog3, type = "h", ylab = "count",
main = "X teeth smooth")
# bivariate example, preserving first 3 moments of total
# and v (anchor) each of x and y, and the covariance
# between anchor and total
# see equated scores in Wang (2009), Table 4
xvtab <- freqtab(KBneat$x[, 1], KBneat$x[, 2],
xscale = 0:36, vscale = 0:12)
yvtab <- freqtab(KBneat$y[, 1], KBneat$y[, 2],
xscale = 0:36, vscale = 0:12)
Y <- yvtab[, 1]
V <- yvtab[, 2]
scorefun <- cbind(Y, Y^2, Y^3, V, V^2, V^3, V * Y)
wang09 <- equate(xvtab, yvtab, type = "equip",
method = "chained", smooth = "loglin", xscorefun = scorefun,
yscorefun = scorefun)
wang09$concordance
# replicate Moses and von Davier, 2006, univariate example:
uv <- c(0, 4, 11, 16, 18, 34, 63, 89, 87, 129, 124,
154, 125, 131, 109, 98, 89, 66, 54, 37, 17)
loglinear(as.freqtab(cbind(0:20, uv)), degree = 3)
Run the code above in your browser using DataLab