여러 종에 대해 개별적으로 GLM을 신청하거나 신청하는 방법은 무엇입니까?
Aug 21 2020
데이터 세트의 여러 종에서 GLM을 실행하려고합니다. 현재 나는 각 종에 대한 내 데이터를 하위 설정하고이 코드를 복사하고 있으며 상당히 혼란스러워졌습니다. 이 작업을 수행하는 더 좋은 방법이 있어야한다는 것을 알고 있지만 (아마도 lapply 기능으로?) 어떻게 시작해야할지 모르겠습니다.
종에 대한 CPUE (단위 노력 당 포획)에서 모델을 실행하고 있으며 설명 변수로 Year, Salinity, Discharge 및 Rainfall을 사용합니다.
내 데이터는 다음과 같습니다. https://drive.google.com/file/d/1_ylbMoqevvsuucwZn2VMA_KMNaykDItk/view?usp=sharing
이것은 내가 시도한 코드입니다. 작업은 완료되지만 매번이 코드를 복사하고 종을 변경했습니다. 이 프로세스를 단순화하고 코드를 약간 정리하는 방법을 찾고 싶습니다.
fish_df$pinfishCPUE <- ifelse(fish_df$Commonname == "Pinfish", fish_all$CPUE, 0) #create binomial column fish_df$binom <- ifelse(fish_df$pinfishCPUE > 0, 1,0)
glm.full.bin = glm(binom~Year+Salinity+Discharge +Rainfall,data=fish_df,family=binomial)
glm.base.bin = glm(binom~Year,data=fish_df,family=binomial)
#step to simplify model and get appropriate order
glm.step.bin = step(glm.base.bin,scope=list(upper=glm.full.bin,lower=~Year),direction='forward',
trace=1,k=log(nrow(fish_df)))
#final model - may choose to reduce based on deviance and cutoff in above step
glm.final.bin = glm.step.bin
print(summary(glm.final.bin))
#calculate the LSMeans for the proportion of positive trips
lsm.b.glm = emmeans(glm.final.bin,"Year",data=fish_df)
LSMeansProp = summary(lsm.b.glm)
산출:
Call:
glm(formula = log.CPUE ~ Month + Salinity + Temperature, family = gaussian,
data = fish_B_pos)
Deviance Residuals:
Min 1Q Median 3Q Max
-3.8927 -0.7852 0.1038 0.8974 3.5887
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.38530 0.72009 3.313 0.00098 ***
Month 0.10333 0.03433 3.010 0.00272 **
Salinity -0.13530 0.01241 -10.900 < 2e-16 ***
Temperature 0.06901 0.01434 4.811 1.9e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for gaussian family taken to be 1.679401)
Null deviance: 1286.4 on 603 degrees of freedom
Residual deviance: 1007.6 on 600 degrees of freedom
AIC: 2033.2
Number of Fisher Scoring iterations: 2
답변
1 Duck Aug 21 2020 at 04:56
모델에 대한 함수를 만든 다음 변수별로 데이터 프레임에 lapply
적용한 결과 목록을 통해 다음 접근 방식을 제안 split()
합니다 Commonname
.
library(emmeans)
#Load data
fish_df <- read.csv('fish_df.csv',stringsAsFactors = F)
#Code
List <- split(fish_df,fish_df$Commonname) #Function for models mymodelfun <- function(x) { #Create binomial column x$binom <- ifelse(x$pinfishCPUE > 0, 1,0)
glm.full.bin = glm(binom~Year+Salinity+Discharge +Rainfall,data=x,family=binomial)
glm.base.bin = glm(binom~Year,data=x,family=binomial)
#step to simplify model and get appropriate order
glm.step.bin = step(glm.base.bin,scope=list(upper=glm.full.bin,lower=~Year),direction='forward',
trace=1,k=log(nrow(x)))
#final model - may choose to reduce based on deviance and cutoff in above step
glm.final.bin = glm.step.bin
print(summary(glm.final.bin))
#calculate the LSMeans for the proportion of positive trips
lsm.b.glm = emmeans(glm.final.bin,"Year",data=x)
LSMeansProp = summary(lsm.b.glm)
return(LSMeansProp)
}
#Apply function
Lmods <- lapply(List,mymodelfun)
에서 Lmods
모델, 여기 예제의 결과가있을 것입니다 :
Lmods$`Atlantic Stingray`
산출:
Year emmean SE df asymp.LCL asymp.UCL
2009 -22.6 48196 Inf -94485 94440
Results are given on the logit (not the response) scale.
Confidence level used: 0.95