Three-dimensional contingency table

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Three-dimensional contingency table

Randklev, Charles
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)
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Three-dimensional contingency table

David Winsemius

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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Three-dimensional contingency table

Ista Zahn-2
In reply to this post by Randklev, Charles
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
<[hidden email]> 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)
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Loading...