tdata = data.frame(x = seq(-1, 1, len = (nn <- 1000)))
foo = function(x) 1 + 4*x
tdata = transform(tdata, ystar = foo(x) + rnorm(nn))
Lower = 1; Upper = 4
tdata = transform(tdata, y = pmax(ystar, Lower))
tdata = transform(tdata, y = pmin(y, Upper))
with(tdata, table(y == Lower | y == Upper)) # How many censored values?
fit = vglm(y ~ x, tobit(Lower = Lower, Upper = Upper), tdata, trace = TRUE)
table(fit@extra$censoredL)
table(fit@extra$censoredU)
coef(fit, matrix = TRUE)
summary(fit)
with(tdata, plot(x, y, main = "Tobit model", las=1))
legend(-0.9, 3, c("Truth", "Estimate"), col=c("blue", "red"), lwd=2)
with(tdata, lines(x, foo(x), col="blue", lwd=2))
with(tdata, lines(x, fitted(fit), col="red", lwd=2, lty="dashed"))
Run the code above in your browser using DataLab