# Example 1
# Fit the proportional odds model, p.179, in McCullagh and Nelder (1989)
data(pneumo)
pneumo = transform(pneumo, let=log(exposure.time))
(fit = vglm(cbind(normal, mild, severe) ~ let,
cumulative(parallel=TRUE, reverse=TRUE), pneumo))
fit@y # Sample proportions
weights(fit, type="prior") # Number of observations
coef(fit, matrix=TRUE)
constraints(fit) # Constraint matrices
# Example 2
# Zero-inflated Poisson model
x = runif(n <- 2000)
phi = logit(-0.5 + 1*x, inverse=TRUE)
lambda = loge(0.5 + 2*x, inverse=TRUE)
y = rzipois(n, lambda, phi)
table(y)
fit = vglm(y ~ x, zipoisson, trace=TRUE)
coef(fit, matrix=TRUE) # These should agree with the above values
# Example 3
# Fit a two species GAM simultaneously
data(hunua)
fit2 = vgam(cbind(agaaus, kniexc) ~ s(altitude, df=c(2,3)),
binomialff(mv=TRUE), hunua)
coef(fit2, mat=TRUE) # Not really interpretable
plot(fit2, se=TRUE, overlay=TRUE, lcol=1:2, scol=1:2)
attach(hunua)
o = order(altitude)
matplot(altitude[o], fitted(fit2)[o,], type="l", lwd=2, las=1,
xlab="Altitude (m)", ylab="Probability of presence",
main="Two plant species' response curves", ylim=c(0,.8))
rug(altitude)
detach(hunua)
# Example 4
# LMS quantile regression
data(bminz)
fit = vgam(BMI ~ s(age, df=c(4,2)), fam=lms.bcn(zero=1), data=bminz, tr=TRUE)
predict(fit)[1:3,]
fitted(fit)[1:3,]
bminz[1:3,]
# Person 1 is near the lower quartile of BMI amongst people his age
cdf(fit)[1:3]
# Quantile plot
par(mfrow=c(1,1), bty="l", mar=c(5,4,4,3)+0.1, xpd=TRUE)
qtplot(fit, percentiles=c(5,50,90,99), main="Quantiles",
xlim=c(15,90), las=1, ylab="BMI", lwd=2, lcol=4)
# Density plot
ygrid = seq(15, 43, len=100) # BMI ranges
par(mfrow=c(1,1), lwd=2)
a = deplot(fit, x0=20, y=ygrid, xlab="BMI", col="black",
main="Density functions at Age = 20 (black), 42 (red) and 55 (blue)")
a
a = deplot(fit, x0=42, y=ygrid, add=TRUE, llty=2, col="red")
a = deplot(fit, x0=55, y=ygrid, add=TRUE, llty=4, col="blue", Attach=TRUE)
a@post$deplot # Contains density function values
# Example 5
# GEV distribution for extremes
data(oxtemp)
(fit = vglm(maxtemp ~ 1, egev, data=oxtemp, trace=TRUE))
fitted(fit)[1:3,]
coef(fit, mat=TRUE)
Coef(fit)
vcov(fit)
vcov(fit, untransform=TRUE)
sqrt(diag(vcov(fit))) # Approximate standard errors
rlplot(fit)
Run the code above in your browser using DataLab