Hi, I am trying to assemble a three-way contingency table examining the presence/absence of mussels, water depth (Depth1 and Depth 2) and water velocity (Flow vs. No Flow). I have written the following code listed below; however, when run the glm I get the following message, "Error in model.frame.default(formula = Count ~ MP + wd + wv, drop.unused.levels = TRUE) : variable lengths differ (found for 'MP')". This may be something simple, if so I apologize. Any help would be greatly appreciated. Best, C.R. numbers <- c(1134,956,328,529,435,599,27,99) dim(numbers) <- c(2,2,2) numbers dimnames(numbers)[[3]] <-list("Mussels", "No Mussels") dimnames(numbers)[[2]] <- list("Flow", "No Flow") dimnames(numbers)[[1]] <- list("Depth1", "Depth2") ftable(numbers) as.data.frame.table(numbers) frame <- as.data.frame.table(numbers) names(frame) <- c("wd", "wv", "MP", "Count") frame attach(frame) model1 <- glm(Count~MP+wd+wv,poisson)
On Aug 28, 2010, at 10:58 PM, Randklev, Charles wrote:> Hi, > > I am trying to assemble a three-way contingency table examining the > presence/absence of mussels, water depth (Depth1 and Depth 2) and > water velocity (Flow vs. No Flow). I have written the following code > listed below; however, when run the glm I get the following message, > "Error in model.frame.default(formula = Count ~ MP + wd + wv, > drop.unused.levels = TRUE) : variable lengths differ (found for > 'MP')". This may be something simple, if so I apologize. Any help > would be greatly appreciated.> > Best, > C.R. > > numbers <- c(1134,956,328,529,435,599,27,99) > dim(numbers) <- c(2,2,2) > numbers > dimnames(numbers)[[3]] <-list("Mussels", "No Mussels") > dimnames(numbers)[[2]] <- list("Flow", "No Flow") > dimnames(numbers)[[1]] <- list("Depth1", "Depth2") > ftable(numbers) > as.data.frame.table(numbers) > frame <- as.data.frame.table(numbers) > names(frame) <- c("wd", "wv", "MP", "Count") > frame > attach(frame) > model1 <- glm(Count~MP+wd+wv,poisson)I don't get that error, so maybe you have more than one MP variable after attaching "frame". Have you used ls() recently. If there is an extra "MP" you should see it there but it shouldn't show up if it is only attached. The attach function is a common cause of perplexing errors. -- David.
I didn't see any error when I ran your code in my computer:> numbers <- c(1134,956,328,529,435,599,27,99) > dim(numbers) <- c(2,2,2) > numbers, , 1 [,1] [,2] [1,] 1134 328 [2,] 956 529 , , 2 [,1] [,2] [1,] 435 27 [2,] 599 99> dimnames(numbers)[[3]] <-list("Mussels", "No Mussels") > dimnames(numbers)[[2]] <- list("Flow", "No Flow") > dimnames(numbers)[[1]] <- list("Depth1", "Depth2") > ftable(numbers)Mussels No Mussels Depth1 Flow 1134 435 No Flow 328 27 Depth2 Flow 956 599 No Flow 529 99> as.data.frame.table(numbers)Var1 Var2 Var3 Freq 1 Depth1 Flow Mussels 1134 2 Depth2 Flow Mussels 956 3 Depth1 No Flow Mussels 328 4 Depth2 No Flow Mussels 529 5 Depth1 Flow No Mussels 435 6 Depth2 Flow No Mussels 599 7 Depth1 No Flow No Mussels 27 8 Depth2 No Flow No Mussels 99> frame <- as.data.frame.table(numbers) > names(frame) <- c("wd", "wv", "MP", "Count") > framewd wv MP Count 1 Depth1 Flow Mussels 1134 2 Depth2 Flow Mussels 956 3 Depth1 No Flow Mussels 328 4 Depth2 No Flow Mussels 529 5 Depth1 Flow No Mussels 435 6 Depth2 Flow No Mussels 599 7 Depth1 No Flow No Mussels 27 8 Depth2 No Flow No Mussels 99> attach(frame) > model1 <- glm(Count~MP+wd+wv,poisson) > summary(model1)Call: glm(formula = Count ~ MP + wd + wv, family = poisson) Deviance Residuals: 1 2 3 4 5 6 7 8 2.5545 -7.0682 -0.1343 7.4889 1.0555 5.7533 -11.0107 -4.2551 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 6.95668 0.02631 264.414 < 2e-16 *** MPNo Mussels -0.93237 0.03466 -26.900 < 2e-16 *** wdDepth2 0.12629 0.03127 4.039 5.37e-05 *** wvNo Flow -1.15626 0.03657 -31.618 < 2e-16 *** --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 2279.76 on 7 degrees of freedom Residual deviance: 286.14 on 4 degrees of freedom AIC: 355.18 Number of Fisher Scoring iterations: 4 -- View this message in context: http://r.789695.n4.nabble.com/Three-dimensional-contingency-table-tp2398915p2399062.html Sent from the R help mailing list archive at Nabble.com.
Hi, Your example works fine for me. My guess is that you have one of wd, wv, MP, or Count defined as a global variable. This is the main reason the use of attach() is discouraged by many people on this list. The safer thing to do is model1 <- glm(Count~MP+wd+wv,poisson. data = frame) -Ista On Sat, Aug 28, 2010 at 10:58 PM, Randklev, Charles <CharlesRandklev at my.unt.edu> wrote:> Hi, > > I am trying to assemble a three-way contingency table examining the presence/absence of mussels, water depth (Depth1 and Depth 2) and water velocity (Flow vs. No Flow). I have written the following code listed below; however, when run the glm I get the following message, "Error in model.frame.default(formula = Count ~ MP + wd + wv, drop.unused.levels = TRUE) : variable lengths differ (found for 'MP')". This may be something simple, if so I apologize. Any help would be greatly appreciated. > > Best, > C.R. > > numbers <- c(1134,956,328,529,435,599,27,99) > dim(numbers) <- c(2,2,2) > numbers > dimnames(numbers)[[3]] <-list("Mussels", "No Mussels") > dimnames(numbers)[[2]] <- list("Flow", "No Flow") > dimnames(numbers)[[1]] <- list("Depth1", "Depth2") > ftable(numbers) > as.data.frame.table(numbers) > frame <- as.data.frame.table(numbers) > names(frame) <- c("wd", "wv", "MP", "Count") > frame > attach(frame) > model1 <- glm(Count~MP+wd+wv,poisson) > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org