library(MASS)
## lm
## unrestricted linear model for ages (in months) at which an
## infant starts to walk alone.
# prepare data
DATA <- subset(ZelazoKolb1972, Group != "Control")
# fit unrestrikted linear model
fit1.lm <- lm(Age ~ Group, data = DATA)
# some artificial restrictions
H1 <- "GroupPassive > 0; GroupPassive < GroupNo"
H2 <- "GroupPassive > 0; GroupPassive > GroupNo"
H3 <- "GroupPassive = 0; GroupPassive < GroupNo"
# object is of class lm
goric(fit1.lm, constraints = list(H1 = H1, H2 = H2, H3 = H3))
# same result, but using objects of class restriktor
fit1.con <- restriktor(fit1.lm, constraints = H1)
fit2.con <- restriktor(fit1.lm, constraints = H2)
fit3.con <- restriktor(fit1.lm, constraints = H3)
goric(fit1.con, fit2.con, fit3.con)
# same result, but using the parameter estimates and covariance matrix as input
# Note, that in case of a numeric input only the gorica(c) can be computed.
goric(coef(fit1.lm), VCOV = vcov(fit1.lm), constraints = list(H1 = H1, H2 = H2, H3 = H3))
# hypothesis H1 versus the complement (i.e., not H1)
goric(fit1.lm, constraints = list(H1 = H1), comparison = "complement")
## GORICA
# generate data
n <- 10
x1 <- rnorm(n)
x2 <- rnorm(n)
y <- 1 + x1 + x2 + rnorm(n)
# fit unconstrained linear model
fit.lm <- lm(y ~ x1 + x2)
# extract unconstrained estimates
est <- coef(fit.lm)
# unconstrained variance-covariance matrix
VCOV <- vcov(fit.lm)
## constraint syntax (character)
h1 <- "x1 > 0"
h2 <- "x1 > 0; x2 > 0"
# use fitted unconstrained linear model
goric(fit.lm, constraints = list(h1, h2), type = "gorica")
# use unconstrained estimates
goric(est, VCOV = VCOV, constraints = list(h1, h2), type = "gorica")
## constraint syntax (matrix notation)
h1 <- list(amat = c(0,1,0))
h2 <- list(amat = rbind(c(0,1,0), c(0,0,1)), rhs = c(0.5, 1), neq = 0)
goric(fit.lm, constraints = list(h1, h2), type = "gorica")
goric(est, VCOV = VCOV, constraints = list(h1, h2), type = "gorica")
## use parallel processing for computing the level probabilities (i.e., mixing weights)
if (FALSE) {
goric(fit.lm, constraints = list(H1), comparison = "complement",
mix.weights = "boot", parallel = "snow", ncpus = 8, mix.bootstrap = 99999)
}
Run the code above in your browser using DataLab