# \donttest{
library(ggplot2)
data(phosphorus)
n = nrow(phosphorus)
ggplot(phosphorus) + geom_line(aes(x=1:n, y=lP)) +
geom_line(aes(x=1:n, y=lcl), color="red", linetype="dashed") +
labs(x="Time") + theme_bw()
# Proportion of censoring
prop.table(table(phosphorus$cc))
# A censored regression model
x = cbind(1, phosphorus$lQ)
cc = phosphorus$cc
lcl = rep(-Inf, n)
ucl = phosphorus$lcl
miss = which(is.na(phosphorus$lP))
cc[miss] = 1
ucl[miss] = Inf
# Fitting a model with normal innovations
set.seed(8765)
mod1 = ARCensReg(cc, lcl, ucl, phosphorus$lP, x, p=1, tol=.001)
# Fitting a model with Student-t innovations
set.seed(287399)
mod2 = ARtCensReg(cc, lcl, ucl, phosphorus$lP, x, p=1, tol=.001)
# Plotting observed and imputed values
data.plot = data.frame(y=phosphorus$lP, ynorm=mod1$yest, yt=mod2$yest)
#
ggplot(data.plot) + geom_line(aes(x=1:n, y=ynorm), color=4) +
geom_line(aes(x=1:n, y=yt), color="deeppink", linetype="dashed") +
geom_line(aes(x=1:n, y=y)) + labs(x="Time", y="lP") + theme_bw()
# Imputed values
data.plot[cc==1,]
# }
Run the code above in your browser using DataLab