#Repeatability Exercise #Alex Trillo October 27,2020 #Introduction-We are going to use this script to find our repeatability scores ##1. Setting your working directory #Before we do anything: Do you know where everything is going to be saved to? #Do you know your working directory? Use "getwd() to figure out where you are getwd() #The next lines of code should define your "working directory". This is a folder #on your computer where R will look for data, save plots, etc. To make your #workflow easier, it is good practice to save everything related to one project #in the same place. To change your working directory to the folder you have #made with the repeatability data and this script: use "setwd()" and enter #inside the brackets the filepath to the folder in your computer. #In my case (I have a mac) it looks like this: #setwd("/Users/ptrillo/desktop/Animal Behavior 2020_02Fall/Lab/Week11_Repeatability Measurements") #getwd() #Watch out! Note that on a Windows computer, a copied-and-pasted file path will have #backslashes separating the folders ("C:\folder\data"), but the filepath you enter #into R should use forward slashes ("C:/folder/data"). You need to change those by hand #You can also set the working directory from the file navigator on the lower right #By first choosing the folder you want to be in, and then selecting "more" and then #"set as working directory" ##2. Uploading your .csv dataset to R studio #OK. Now that you have a folder ready to work, let's upload your data #First, you need to save your excel sheet as a comma delimited frame #Open your excel sheet choose "save as" -> for file format - choose "CSV" #Name your .csv file "RepeatabilityData.csv" #Upload your CSV dataset by going to the "file" menu -> import dataset" -> "from text(base)" #Find your dataset "RepeatabilityData.csv" #Make sure you say "yes to headings" #You should be able to see your dataset as another window - check that everything is there! ##3. Installing the packages we need: #To calculate a one-way ANOVA, we need to install a special package call lme4 package install.packages("lme4") library(lme4) ##4. Looking at your data #First make sure your data looks OK head(RepeatabilityData) #gives you the first six rows str(RepeatabilityData) #gives youn information about your variables #you can turn "butterfly_image_number" into a factor RepeatabilityData$butterfly_image_number <- as.factor(RepeatabilityData$butterfly_image_number) class(RepeatabilityData$butterfly_image_number) class(RepeatabilityData$repeated_measure_number) str(RepeatabilityData) #now "butterfly_image_number" is a factor ##5. Using a one-way ANOVA test to calculate repeatability #Repeatability is calculated from an Analysis of Variance (ANOVA). If you have had statistics, #more power to you, but this exercise assumes little stats background. #Just go along with the idea that ANOVA can, in fact, partition variation into a part that is due to #variation within individuals and a part that is due to variation among individuals. #Now we need to run a one-factor ANOVA - To do this in R, we first need to use the #lm (linear model) function to specify the model we want to run - which is an ANOVA #We use a formula to specify the model - this is just the syntax ##6. First measurement - "RWingLength" #lm_repeat1<- lm(RWingLength ~ butterfly_image_number, data = RepeatabilityData) #lm_repeat1 is the name of our model #in the lm formula, in parenthesis we specify our response variable "RWingLength" #and on the right side of the tilde we have the explanatory variable "butterfly_image_number" #then we specify what dataframe all the data comes from with "data = RepeatabilityData" lm_repeat1<- lm(RWingLength ~ butterfly_image_number, data = RepeatabilityData) #Now, to produce an ANOVA table with an output of the results we run the anova function on #the name of our model #anova(lm_repeat1) anova(lm_repeat1) #You should have a table that looks similar to this: #Analysis of Variance Table #Response: RWingLength # Df Sum Sq Mean Sq F value Pr(>F) #butterfly_image_number 3 92.273 30.7575 2450 < 2.2e-16 *** #Residuals 12 0.151 0.0126 #--- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 #Notice first that this table gives you a statistical test of whether or not a significant #amount of the total variation is found by differences BETWEEN the different groups (individuals #in this case) RELATIVE to the amount of variation WITHIN groups (or individuals in this case) #The statistic is the F value, and the P-value is the likelihood of finding as big a difference #between groups as you found even IF there was no true difference. As you see here, the probability #is really small (less than 0.05) and we can say that there is a significant difference between #these individuals. #But we are interested in the repeatability of our measurements, so from there, #use the Mean sq for residuals and M sq for butterfly_image_number #to calculate repeatability for RWingLength with the equation in the handout! ##7. Now let's do our second measurement - "RWingWidth" #We will use the same syntax, except this time the name of our model is lm_repeat2 #lm_repeat2<- lm(RWingWidth ~ butterfly_image_number, data = RepeatabilityData) #in the lm formula, in parenthesis we specify our response variable "RWingWidth" #and on the right side of the tilde we have the explanatory variable "butterfly_image_number" #then we specify what dataframe the all the data comes from with "data = RepeatabilityData" lm_repeat2<- lm(RWingWidth ~ butterfly_image_number, data = RepeatabilityData) #Now, to produce an ANOVA table with an output of the results we run the anova function on #the name of our model #anova(lm_repeat2) anova(lm_repeat2) #from this new table, use the Mean sq for residuals and M sq for butterfly_image_number #to calculate repeatability for RWingWidth with the equation in the handout! ##8. Now let's do our third measurement - "RAnteriorSpotM3" #We will use the same syntax, except this time the name of our model is lm_repeat3 lm_repeat3<- lm(RAnteriorSpotM3 ~ butterfly_image_number, data = RepeatabilityData) #Now, to see the table #anova(lm_repeat3) anova(lm_repeat3) #from this new table, use the Mean sq for residuals and M sq for butterfly_image_number #to calculate repeatability for RAnteriorSpotM3 with the equation in the handout! ##9. Now use the examples above and try to write the code for the very last measurement #"RBlackPatchApex", and use the table to calculate your score for this measurement.