Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

r - Calculating Survival rate from month to month without losing starting values

I have a set of code that divides the number of alive specimens from the initial count. I am trying to determine the survival rate for the entire 5 month experiment but, there seems to be an issue with the computation each month. For the initial month, the code computes the correct survival rate (ie: 48/50 - 96%). But, the issue comes in when computing for the next month where the code will compute the survival rate from 48 instead of 50 (ie: 46/48 survived, instead of 46/50 which is what I need). It continues this way for the remainder of the experiment (30/46 month 3, then 20/30 for month 4).

Additionally, each of the "dead" specimens are then added to an NA group automatically (There should be no NA groups). I think if the first issue is taken care of then the NA issue wont happen. Is there a way to fix this with the code I have or do I need to rearrange the data in excel?

I have 2 species in 4 habitats that need this code for analysis.

Thanks!

Month 1
| Species  |       Cage     || nStart   |     nAlive     ||  PropAlive
| -------- | -------------- ||          |                |
| X        | 1              ||   10     |        9       |     .9
| Y        | 2              ||   10     |        8       |     .8
| -------- | -------------- ||          |                |
Month 2
| Species  |       Cage     || nStart   |     nAlive     ||  PropAlive (nAlive/nStart)
| -------- | -------------- ||          |                |
| X        | 1              ||   9      |        8       |     .89
| Y        | 2              ||   8      |        7       |     .875
| -------- | -------------- ||          |                |

month 2 should be 8/10 and 7/10 for Prop Alive not 8/9 and 7/8.

    library(readxl)
library(tidyverse)
library(lme4)
library(car)
library(emmeans)

JulyData<- read_excel("~/R/Cage Data Final 2016 EMV 1.20.xlsx", sheet="7.1.2016")
  str(JulyData)
summary(JulyData$Lice)

AllCages<- distinct(JulyData, Cage, Species)
  AllCages$nStart<- rep(10,nrow(AllCages))

Alive<- JulyData%>%
    filter(!is.na(Lice))%>%
    group_by(Cage, Species)%>%
summarise(nAlive=n())
          
CleanData<-merge(AllCages,Alive, all=TRUE)  

CleanData$nAlive[is.na(CleanData$nAlive)]<-0    

CleanData$nAlive[CleanData$nAlive>10]<-10

CleanData<-CleanData %>%
  separate(Cage,c("Habitat", "Rep"),1,remove=FALSE) %>%
  mutate(nDead=nStart-nAlive)
CleanData

CleanData%>%
  group_by(Species,Habitat)%>%
  summarize(nStart=sum(nStart),
      nAlive=sum(nAlive),
      PropAlive = nAlive/nStart)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So, the issue was related to formatting within the data. The code is right - a simple labeling error was the issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...