使用glm计算列值

时间:2015-01-24 01:28:00

标签: r glm

我有一个包含大约20,000行数据的数据集。数据按年份和面积分组。我正在计算50%的虾是女性的长度。当我运行以下代码时,它只返回整个数据集的值,而不是每年/区域对的唯一值。

数据集名为shrimp

library(MASS)
library(plyr)

数据

shrimp <- structure(list(cruise = c(1972L, 1972L, 1972L, 1972L, 1972L, 
1972L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 
1985L, 1985L, 1985L, 1985L, 1985L), areaname = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Balboa", 
"Chiniak", "Pavlof"), class = "factor"), size = c(9, 9.5, 10, 
10.5, 11, 11.5, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 
20, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19
), male = c(49L, 61L, 92L, 57L, 46L, 9L, 151L, 64L, 30L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 638L, 625L, 666L, 447L, 393L, 214L, 119L, 
33L, 12L, 13L, 7L, 6L), female = c(0L, 0L, 0L, 0L, 0L, 0L, 3L, 
15L, 35L, 78L, 122L, 105L, 76L, 28L, 13L, 36L, 0L, 5L, 8L, 13L, 
38L, 60L, 54L, 38L, 28L, 93L, 131L, 195L), total = c(49L, 61L, 
92L, 57L, 46L, 9L, 154L, 79L, 65L, 78L, 123L, 105L, 76L, 28L, 
13L, 36L, 638L, 630L, 674L, 460L, 431L, 274L, 173L, 71L, 40L, 
106L, 138L, 201L)), .Names = c("cruise", "areaname", "size", 
"male", "female", "total"), row.names = c(NA, 28L), class = "data.frame")

模型

cdata <- ddply(shrimp, c("cruise", "areaname"), summarise, 
                    female = dose.p(glm(cbind(female, males) ~ size,
                    family=binomial(logit), data=shrimp), cf=1:2, p =.5))

1 个答案:

答案 0 :(得分:2)

没有什么可以针对(还)测试这个,但也许:

library(MASS)
library(plyr)
cdata <- ddply(shrimp, c("cruise", "areaname"), summarise, 
           female = dose.p( glm(cbind(female, male) ~ size, 
                         family=binomial("logit") ),  cf=1:2, p = 0.5)
               ) 
##   cruise areaname   female
## 1   1972   Balboa 67.15131
## 2   1985   Pavlof 16.88196
## 3   2003  Chiniak 16.37031