Jason Hwa wrote:> Dear R-help community,
>
> I am working with multidimensional contingency tables and I am having
> trouble getting loglm to run on all dimensions without typing out each
> dimension.
>
> I have generated random data and provided output for the results I want
> below:
>
> d1.c1 <- rnorm(20, .10, .02)
> d1.c2 <- rnorm(20, .10, .02)
> d2.c1 <- rnorm(20, .09, .02)
> d2.c2 <- rnorm(20, .09, .02)
> d3.c1 <- rnorm(20, .11, .02)
> d3.c2 <- rnorm(20, .11, .02)
>
> group1 <- cbind(1, d1.c1, d2.c1, d3.c1)
> group2 <- cbind(2, d1.c2, d2.c2, d3.c2)
>
> colnames(group1) <- colnames(group2) <- c("group",
"dim1", "dim2", "dim3")
> combined <- rbind(group1, group2)
> combined[,2:4] <- combined[,2:4] > .1
>
> ctables <- xtabs(~., data = combined)
> loglm(~group+dim1+dim2+dim3, data=ctables)
>
> Call:
> loglm(formula = ~group + dim1 + dim2 + dim3, data = ctables)
>
> Statistics:
> X^2 df P(> X^2)
> Likelihood Ratio 12.29856 11 0.3416253
> Pearson 10.28058 11 0.5053391
>
> However, the number and the names of the dimensions change for each
> dataset. What I want is to be able to run the following line at the end
> of the code: "loglm(~., data=ctables)", but it always prints the
> following error:
>
> Error in terms.formula(formula <- denumerate(formula)) :
> '.' in formula and no 'data' argument
>
> Can anyone help me out?
>
> Thank you,
> Jason
>
Presumably you're using loglm from package MASS (which you
should indicate).
To use the dot notation, you need to put your data in
dataframe form. This works:
d <- data.frame(ftable(ctables))
loglm(Freq ~ ., data = d)
Or you could use the integer notation for the formula:
loglm( ~ 1 + 2 + 3 + 4, data = ctables)
You could generate the formula in your code:
form <- as.formula(paste("~", paste(1:4,
collapse="+")))
and then use
loglm(form, data = ctables)
-Peter Ehlers