set.seed(2010)
x <- round(rnorm(1000,100,15))
xscale <- 50:150
# smooth x preserving first 3 moments:
xlog1 <- loglinear(freqtab(x,xscale),degree=3)
xtab <- freqtab(x,xscale)
cbind(xtab,xlog1$fit)
par(mfrow=c(2,1))
plot(xscale,xtab[,2],type="h",ylab="count",
main="X raw")
plot(xscale,xlog1$fit,type="h",ylab="count",
main="X smooth")
# 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 <- freqtab(xt,xscale,add=TRUE)
xlog2 <- loglinear(xttab,degree=3)
cbind(xscale,xt,xlog2$fit)
# 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$fit)
par(mfrow=c(2,1))
plot(xscale,xt,type="h",ylab="count",
main="X teeth raw")
plot(xscale,xlog3$fit,type="h",ylab="count",
main="X teeth smooth")
# bivariate example, preserving first 3 moments of y
# and v (anchor) each, the covariance of y and v, and
# 3 additional degrees of dependence in y and v:
yv <- KBneat$y
yscale <- 0:36
vscale <- 0:12
yvtab <- freqtab(yv[,1],yscale,yv[,2],vscale)
Y <- yvtab[,1]
V <- yvtab[,2]
scorefun <- cbind(Y,Y^2,Y^3,V,V^2,V^3,V*Y,V*Y*Y,V*V*Y,V*V*Y*Y)
loglinear(yvtab,scorefun)$model
# 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(freqtab(uv,0:20,add=TRUE),degree=3)
Run the code above in your browser using DataLab