我想检验一个变量是否对不同物种的渔获率有影响,但我很难理解如何以一种整洁和简洁的方式做到这一点。我有一个大约400个渔获率的数据集,但是物种的捕获率之间有很大的差异。看起来是这样的:
set.seed(42)
n <- 100
df<- data.frame(organization=rep(LETTERS[1:4], n/2),
species=rep(c("shark", "whale", "fish", "ray", "turtle"), each=20) ,
gear=rep(c("l", "p", "l", "p", "l", "p", "l", "p", "l", "p"), each =10),
rate=rnorm(n))到目前为止,我尝试的是:
library(broom)
df %>%
group_by(species, gear) %>%
do(tidy(lm(rate~organization, data=.))) %>%
mutate(p.value=round(p.value, 3)) %>%
filter(p.value<0.05)#filter only sig. pvals我想知道的是,是否有一种简单而优雅的方法来测试唯一组织的效果,但同时仍然对物种和齿轮进行分组。从本质上说,物种和齿轮有很大的影响,不同的物种之间并不能真正进行比较。所以我想知道,在同一个物种和装备中,组织是否会产生不同的影响。
任何帮助都将不胜感激!
发布于 2021-11-27 23:18:36
这是个开始。不是完全的解决方案。在这里,我们只使用species进行分组。您可以先根据species进行分组,然后再通过gear进行分组,然后将两个group_by(species, gear)组合起来。
library(tidyverse)
library(broom)
df %>%
mutate(species = as_factor(species)) %>%
group_by(species) %>%
group_split() %>%
map_dfr(.f = function(df) {
lm(rate ~ organization, data = df) %>%
glance() %>%
add_column(species = unique(df$species), .before = 1)
}) species r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual nobs
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int>
1 shark 0.229 0.165 1.18 3.57 0.0234 3 -61.4 133. 141. 50.5 36 40
2 whale 0.192 0.124 1.03 2.84 0.0513 3 -55.6 121. 130. 37.8 36 40
3 fish 0.0980 0.0229 0.999 1.30 0.288 3 -54.6 119. 128. 35.9 36 40
4 ray 0.121 0.0481 0.783 1.66 0.194 3 -44.9 99.7 108. 22.1 36 40
5 turtle 0.0448 -0.0348 0.922 0.563 0.643 3 -51.4 113. 121. 30.6 36 40https://stackoverflow.com/questions/70138783
复制相似问题