set.seed(2010)
x <- round(rnorm(1000,100,15))
xscale <- 50:150
# smooth x, preserving first 3 moments:
xlog1 <- loglinear(x,xscale,degree=3)
xtab <- freqtab(x,xscale)
cbind(xtab,xlog1$fit[,1])
par(mfrow=c(2,1))
plot(xscale,xtab[,2],type="h",ylab="count",
main="X raw")
plot(xscale,xlog1$fit[,1],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)
xlog2 <- loglinear(xt,xscale,degree=3)
cbind(xscale,xt,xlog2$fit[,1])
# 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(xt,xscale,xt.fun)
cbind(xscale,xt,xlog3$fit[,1])
par(mfrow=c(2,1))
plot(xscale,xt,type="h",ylab="count",
main="X teeth raw")
plot(xscale,xlog3$fit[,1],type="h",ylab="count",
main="X teeth smooth")
# 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(uv,0:20,degree=3)
Run the code above in your browser using DataLab