# Example 1: negative binomial with Var(Y) = mu + mu^s2, s2 unknown
nn = 500
s2 = 1.5 # Specify this
c2 = 2 - s2
ndata = data.frame(x2 = runif(nn), x3 = runif(nn))
ndata = transform(ndata, mu = exp(2 + 1 * x2 + 0 * x3))
ndata = transform(ndata, y2 = rnbinom(nn, mu=mu, size=mu^c2))
plot(y2 ~ x2, data = ndata, pch = "+", col = 'blue',
main=paste("Var(Y) = mu + mu^", s2, sep=""))
Fit2 = rrvglm(y2 ~ x2 + x3, negbinomial(zero = NULL),
data = ndata, Norrr = NULL)
c2hat = (Coef(Fit2)@A)["log(k)", 1]
s2hat = 2 - c2hat
s2hat # Estimate of s2
# Example 2
data(car.all)
index = with(car.all, Country == "Germany" | Country == "USA" |
Country == "Japan" | Country == "Korea")
scar = car.all[index, ] # standardized car data
fcols = c(13,14,18:20,22:26,29:31,33,34,36) # These are factors
scar[,-fcols] = scale(scar[,-fcols]) # Standardize all numerical vars
ones = matrix(1, 3, 1)
cms = list("(Intercept)" = diag(3), Width = ones, Weight = ones,
Disp. = diag(3), Tank = diag(3), Price = diag(3),
Frt.Leg.Room = diag(3))
set.seed(111)
fit = rrvglm(Country ~ Width + Weight + Disp. + Tank + Price + Frt.Leg.Room,
multinomial, data = scar, Rank = 2, trace = TRUE,
constraints = cms, Norrr = ~ 1 + Width + Weight,
Uncor = TRUE, Corner = FALSE, Bestof = 2)
fit@misc$deviance # A history of the fits
Coef(fit)
biplot(fit, chull = TRUE, scores = TRUE, clty = 2, Ccex = 2,
ccol = "blue", scol = "red", Ccol = "darkgreen", Clwd = 2,
main = "1=Germany, 2=Japan, 3=Korea, 4=USA")
Run the code above in your browser using DataLab