--- title: "Qubes: Introduction to Survival Analysis in R" author: "Megan Black" date: "4/30/2022" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ```{r} # Packages needed: ## if library() doesn't work, you need to install the packages in your Console! ## do this by typing directly into the Console: install.packages("readxl") for example ## THEN come back to your rmd and try the library() function again library(readxl) # This package will allow us to read in excel spreadsheets like csvs library(survival) # The following packages will allow us to model and plot survival library(survminer) library(ggplot2) # Reading in csv/ xlsx ## if you have trouble with this step, make sure that your .Rmd is in the same project/ file on your Computer as the spreadsheets you need to read in. Double check their names, too! Sometimes these data files will download/ export with added numbers in their names. exp1 <- read_excel("Data Exp1.xlsx") # Making sure data looks correct: you should have five columns show up, including "Event" and "Time" colnames(exp1) # Making sure variables aren't factors is.factor(exp1$MicroColony_ID) is.factor(exp1$Treatment) is.factor(exp1$OriginColony_name) is.factor(exp1$Time) is.factor(exp1$Event) # all false ! exp1 # Now, let's look at the data: ``` # Experiment 1: Are impacts of consumer and agricultural Roundup comperable? ```{r} # Create model plotting treatment against survival ## this model will stay the same for other experiments, but you will need to change the data input. ## and make sure when you repeat this step to name your new models, etc something new! model1 <- survfit(Surv(Time, Event) ~ Treatment, data=exp1) model1 # Plot model using ggsurvplot: mycolors <- c("#780A53","#FA5A20","#A4036F","#C74414","#FF74D4") # making my color palette ggsurvplot(model1, palette = mycolors, legend= "right", title= "Experiment 1") ``` ```{r} # Tabulate data into better format tbl1 = table(exp1$Treatment, exp1$Event) tbl1 #visualize # Run a chi-squared test cs1<- chisq.test(tbl1) cs1 #visualize ``` # Experiment 4: (students are expected to code this on their own) ```{r} # Read in .xlsx and visualize data exp4 <- read_excel("Data Exp4.xlsx") colnames(exp4) is.factor(exp4$MicroColony_ID) is.factor(exp4$Treatment) is.factor(exp4$OriginColony_name) is.factor(exp4$Time) is.factor(exp4$Event) exp4 # Create model plotting treatment against survival model4 <- survfit(Surv(Time, Event) ~ Treatment, data=exp4) model4 # Plot model using ggsurvplot ggsurvplot(model4, palette = mycolors, legend= "right", title= "Experiment 4") ``` ```{r} # Tabulate data tbl4 = table(exp4$Treatment, exp4$Event) tbl4 # Run a chi-square test cs4<- chisq.test(tbl4) cs4 ``` # Experiment 5: (students are expected to code this on their own) ```{r} # Read in .xlsx and visualize data exp5 <- read_excel("Data Exp5.xlsx") colnames(exp5) is.factor(exp5$MicroColony_ID) is.factor(exp5$Treatment) is.factor(exp5$OriginColony_name) is.factor(exp5$Time) is.factor(exp5$Event) exp5 # Create model plotting treatment against survival model5 <- survfit(Surv(Time, Event) ~ Treatment, data=exp5) model5 # Plot model using ggsurvplot ggsurvplot(model5, palette = mycolors, legend= "right", title= "Experiment 5") ``` ```{r} # Tabulate data tbl5 = table(exp5$Treatment, exp5$Event) tbl5 # Run a chi-square test cs5<- chisq.test(tbl5) cs5 ``` # Questions: see teacher's version of the lesson for in-depth answers. 1. Based on our findings from the first experiment, what is the difference between agricultural and consumer Roundup? 2. Did your survival plot for experiment four look different compared to the first experiment? How do you interpret the results displayed by this figure? 3. How would you interpret the results of the chi-square test for experiment four? 4. Did your survival plot for experiment five look different than previous ones? How do you interpret the results being displayed? 5. How would you interpret the results of the chi-square test for experiment five? 6. How do your results compare with those of the focal publication? Were your own findings similar or different? 7. With experiments four and five in mind, what are this study's major implications for conservation? Can you identify a call-to-action from these findings? ```