####################################################################################
# Read data
data(Smoking)
dat <- Smoking
####################################################################################
# Table TXBR
# matrix producing 4x2x3x6 table TXBR
atTXBR = MarginalMatrix(c("X","B","R1","R2","R3","R4"), list(c("X","B","R1"), c("X","B","R2"), c("X","B","R3"), c("X","B","R4")), c(2, 3, 5, 5, 5, 5));
atTXBR %*% c(t(ftable(dat))) #list of frequencies in TXBR
bt = ConstraintMatrix(c("T","X","B","R"), list(c("T","X","B"), c("R")), c(4, 2, 3, 5))
model = list(bt,"log", atTXBR)
fit=MarginalModelFit(dat, model, MaxStepSize =.3, MaxSteps=100, ShowProgress=5)
####################################################################################
# Table of means TX
# matrix producing 4x2x3x5 table TXBR
atTXBR = MarginalMatrix(c("X","B","R1","R2","R3","R4"), list(c("X","B","R1"), c("X","B","R2"), c("X","B","R3"), c("X","B","R4")), c(2, 3, 5, 5, 5, 5));
# collapse over variable B
at3 = MarginalMatrix(c("T","X","B","R"), c("T","X","R"), c(4, 2, 3, 5))
atTXB = at3 %*% atTXBR
atTXB %*% c(t(ftable(dat))) #list of frequencies in TXB
coeff = SpecifyCoefficient("Mean", c(1, 2, 3, 4, 5), 8)
bt = ConstraintMatrix(c("T","X"), list(c("T"), c("X")), c(4, 2));
model = list(bt, coeff, atTXB)
SampleStatistics(dat, list(coeff, atTXB), CoefficientDimensions = c(4,2), Labels =c("T", "X"))
fit=MarginalModelFit(dat, model, MaxStepSize =.3, MaxSteps=100, ShowProgress=5, CoefficientDimensions = c(4,2), Labels =c("T", "X"), ShowParameters=TRUE)
####################################################################################
# Table of means TXB
# matrix producing 4x2x3x5 table TXBR
atTXBR = MarginalMatrix(c("X","B","R1","R2","R3","R4"), list(c("X","B","R1"), c("X","B","R2"), c("X","B","R3"), c("X","B","R4")), c(2, 3, 5, 5, 5, 5));
coeff = SpecifyCoefficient("Mean", c(1, 2, 3, 4, 5), 24)
bt = ConstraintMatrix(c("T","X","B"), list(c("T"), c("X"), c("B")), c(4, 2, 3));
model = list(bt, coeff, atTXBR)
SampleStatistics(dat, list(coeff, atTXBR), CoefficientDimensions = c(4,2,3), Labels =c("T","X","B"))
fit=MarginalModelFit(dat, model, MaxStepSize =.3, MaxSteps=100, ShowProgress=5, CoefficientDimensions = c(4,2,3), Labels =c("T","X","B"), ShowParameters=TRUE)
####################################################################################
# Table of means TXBS
# matrix producing 4x2x3x5 table TXBR
atTXBR = MarginalMatrix(c("X","B","R1","R2","R3","R4"), list(c("X","B","R1"), c("X","B","R2"), c("X","B","R3"), c("X","B","R4")), c(2, 3, 5, 5, 5, 5));
# group categories of R:
at2 = MarginalMatrix(c("T","X","B","R"), c("T","X","B","R"), c(4, 2, 3, 5),
SubsetCoding = list(c("T","X","B","R"),list("Identity","Identity","Identity", rbind(c(1, 1, 1, 0, 0), c(0, 0, 0, 1, 1)))));
coeff = SpecifyCoefficient("ConditionalProbabilities", list(c("T","X","B","S"), c("T","X","B"), c(4, 2, 3, 2)));
bt = ConstraintMatrix(c("T","X","B","S"), list(c("T","X","B"), c("T","S"), c("X","S"), c("B","S")), c(4, 2, 3, 2));
model = list(bt, coeff, at2 %*% atTXBR)
SampleStatistics(dat, list(coeff, at2 %*% atTXBR), CoefficientDimensions = c(4,2,3), Labels =c("T","X","B"))
fit=MarginalModelFit(dat, model, MaxStepSize =.8, MaxSteps=100, ShowProgress=5, CoefficientDimensions = c(4,2,3,2), Labels =c("T","X","B","S"), ShowParameters=TRUE)