SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Downloaden Sie, um offline zu lesen
R Code
for
Descriptive
Statistics of
Phenotypic Data
Avjinder Singh Kaler
Steps
1. Reading data into R—the data file includes a header and
“NA” is a missing value.
data_in <‐‐‐ read.table (file="example.dat", header= T, na.string="NA")
2. Getting access to the data frame (all variables will relate to
this data frame).
attach(data_in)
3. Overview of data (mean, median, maximum, minimum, first
and third quartiles, number of missing values).
summary(data_in)
4. Calculating means and standard deviations (2 indicates that
the function is applied to all columns, na.rm=T means that
missing values are removed).
apply(data_in,2,mean,na.rm=T)
apply(data_in,2,sd,na.rm=T)
5. Means or standard deviations can be calculated for data
points grouped by a factor (e.g. year).
aggregate(data_in,list(data$year),FUN=mean)
aggregate(data_in,list(data$year),FUN=sd)
6. Frequencies for a single variable and across two variables.
table(sex)
table(wormy)
table(wormy,sex)
7. Histogram.
hist(FWEC)
8. xy scatter plots.
plot(FWEC)
9. Box plot.
boxplot(FWEC_sex,data=data_in, range=0)
10. Shapiro–Wilk’s tests to check normality of data
distribution.
shapiro.test(FWEC)
11. Checking data distribution with QQ plot—if data are
normally distributed, the plotted data and the line are well
aligned.
qqplot(FWEC)
qqline(FWEC)
12. Data transformation—log, square root, and cube root
transformation.
log_FWEC <‐‐‐ log(FWEC)
sqrt_FWEC <‐‐‐ sqrt(FWEC+1)
cbrt_FWEC <‐‐‐ (FWEC)^(1/3)
13. Box–Cox transformation.
#CodetofindsuitablelambdaforYtothepowerlambda
#download thelibrary(MASS)
#seq(min value, max value, step) defines the range
from which lambda is drawn
boxcox(FWEC_factor(sex)+factor(birth_rearing_
type), lambda = seq(0,1.0,0.01)
savePlot("boxcox","jpeg")
lambda = "insert maximum lambda value in graph here"
trans(FWEC) <- ((FWEC^lambda)-1)/lambda MASS library
14. Checking homogeneity of variances.
#download library (Rcmdr)
library(Rcmdr)
#run the Leven’s test, specifying the vector of
data y and group, the factor across which the variances
are tested (e.g., year)
leveneTest(y,group)
15. Fitting a linear model and ANOVA.
#need to load the "car" package for Type III ANOVA
library(car)
lmod <- lm(cbrt_FWEC_factor(sex))
#Type I ANOVA
anova(lm)
#Type III ANOVA---Note that the first letter in the
commandbelow has to be a capital "A" (ensure that
you loaded the "car" package as shown above)
Anova(lmod, type¼"III")
16. Addressing confounding of explanatory variables in a linear
model.
lmod1 <- lm(cbrt_FWEC_factor(sex)+factor(birth_
type)*factor(rearing_type))
lmod2 <-lm(cbrt_FWEC_factor(sex)+factor(birth_
rearing_type))
17. Check the difference with an ANOVA.
Anova(lmod1,type="III")
Anova(lmod2,type="III")
18. Model comparison using logistic regression for binary data.
logres <- glm(formula=wormy_status_factor(sex) +
factor(birth_rearing_type), family = binomial
(link="logit"))
#producing an analysis-of-deviance table to test
fixed effects
anova(logres,test="Chisq")
#produces the deviance of the model (the lower the
better the fit)
summary(glm(formula=wormy_status_factor(sex) +
factor(birth_rearing_type), family = binomial
(link="logit"))$deviance))
#the difference in deviance can be formally tested
with a loglikelihood ratio test
#install library(lme4)
library(lme4)
#comparing two nested models ("nested" means that
one has one more factor than the other)
logres1 <- lmer(wormy_status_factor(sex)), family
= "binomial", method="Laplace")
logres2 <- lmer(wormy_status_factor(sex) + factor
(birth_rearing_type), family = "binomial",
Method="Laplace")
anova(logres1,logres2)
#to assess the model, plot predicted probability
against observed proportion
#install library(languageR)
library(languageR)
plot.logistics.fit.fnc(logres1,logres2)
19. Model diagnostics.
#the following produces plot of residual vs. fitted
value, QQ plot, and scale-location plot of the
previously tested model 1 (lmod1)
plot(lmod1)
#assessing a logit model for binary data by plotting
the predicted probability against observed
proportions
#download library(languageR)
library(languageR)
plot.logistic.fit.fnc(logres1,data_in)
20. Extracting residuals and writing them to a file—assuming
lmod2 is the model of choice.
res_lmod2 <-residuals(lmod2)
write.table(res_lmod2,file¼"res_FWEC")

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Data handling in r
Data handling in rData handling in r
Data handling in r
 
Handling Missing Values
Handling Missing Values Handling Missing Values
Handling Missing Values
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in R
 
Data Visualization using base graphics
Data Visualization using base graphicsData Visualization using base graphics
Data Visualization using base graphics
 
Manipulating Data using DPLYR in R Studio
Manipulating Data using DPLYR in R StudioManipulating Data using DPLYR in R Studio
Manipulating Data using DPLYR in R Studio
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
Data tidying with tidyr meetup
Data tidying with tidyr  meetupData tidying with tidyr  meetup
Data tidying with tidyr meetup
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
Export Data using R Studio
Export Data using R StudioExport Data using R Studio
Export Data using R Studio
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R Language
 
R seminar dplyr package
R seminar dplyr packageR seminar dplyr package
R seminar dplyr package
 
Merge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RMerge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using R
 
5. working on data using R -Cleaning, filtering ,transformation, Sampling
5. working on data using R -Cleaning, filtering ,transformation, Sampling5. working on data using R -Cleaning, filtering ,transformation, Sampling
5. working on data using R -Cleaning, filtering ,transformation, Sampling
 
R language introduction
R language introductionR language introduction
R language introduction
 
5 R Tutorial Data Visualization
5 R Tutorial Data Visualization5 R Tutorial Data Visualization
5 R Tutorial Data Visualization
 
Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat Sheet
 

Andere mochten auch

Andere mochten auch (9)

Tutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsTutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plots
 
Genome-wide association mapping of canopy wilting in diverse soybean genotypes
Genome-wide association mapping of canopy wilting in diverse soybean genotypesGenome-wide association mapping of canopy wilting in diverse soybean genotypes
Genome-wide association mapping of canopy wilting in diverse soybean genotypes
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
Genome-Wide Association Mapping of Carbon Isotope and Oxygen Isotope Ratios i...
Genome-Wide Association Mapping of Carbon Isotope and Oxygen Isotope Ratios i...Genome-Wide Association Mapping of Carbon Isotope and Oxygen Isotope Ratios i...
Genome-Wide Association Mapping of Carbon Isotope and Oxygen Isotope Ratios i...
 
SAS and R Code for Basic Statistics
SAS and R Code for Basic StatisticsSAS and R Code for Basic Statistics
SAS and R Code for Basic Statistics
 
Sugarcane yield and plant nutrient response to sulfur amended everglades hist...
Sugarcane yield and plant nutrient response to sulfur amended everglades hist...Sugarcane yield and plant nutrient response to sulfur amended everglades hist...
Sugarcane yield and plant nutrient response to sulfur amended everglades hist...
 
Nutrient availability response to sulfur amendment in histosols having variab...
Nutrient availability response to sulfur amendment in histosols having variab...Nutrient availability response to sulfur amendment in histosols having variab...
Nutrient availability response to sulfur amendment in histosols having variab...
 
R Code for EM Algorithm
R Code for EM AlgorithmR Code for EM Algorithm
R Code for EM Algorithm
 
Seed rate calculation for experiment
Seed rate calculation for experimentSeed rate calculation for experiment
Seed rate calculation for experiment
 

Ähnlich wie R code descriptive statistics of phenotypic data by Avjinder Kaler

3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
LeenaChaudhari24
 

Ähnlich wie R code descriptive statistics of phenotypic data by Avjinder Kaler (20)

Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
 
INTRODUCTION TO STATA.pptx
INTRODUCTION TO STATA.pptxINTRODUCTION TO STATA.pptx
INTRODUCTION TO STATA.pptx
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
R Get Started II
R Get Started IIR Get Started II
R Get Started II
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
R data types
R   data typesR   data types
R data types
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
R Cheat Sheet
R Cheat SheetR Cheat Sheet
R Cheat Sheet
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processing
 
ARIMA Models - [Lab 3]
ARIMA Models - [Lab 3]ARIMA Models - [Lab 3]
ARIMA Models - [Lab 3]
 
R and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenR and Visualization: A match made in Heaven
R and Visualization: A match made in Heaven
 
Write a function called float dotproduct (links to an external site.)(float a...
Write a function called float dotproduct (links to an external site.)(float a...Write a function called float dotproduct (links to an external site.)(float a...
Write a function called float dotproduct (links to an external site.)(float a...
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 

Mehr von Avjinder (Avi) Kaler

Mehr von Avjinder (Avi) Kaler (19)

Unleashing Real-World Simulations: A Python Tutorial by Avjinder Kaler
Unleashing Real-World Simulations: A Python Tutorial by Avjinder KalerUnleashing Real-World Simulations: A Python Tutorial by Avjinder Kaler
Unleashing Real-World Simulations: A Python Tutorial by Avjinder Kaler
 
Tutorial for Deep Learning Project with Keras
Tutorial for Deep Learning Project  with KerasTutorial for Deep Learning Project  with Keras
Tutorial for Deep Learning Project with Keras
 
Tutorial for DBSCAN Clustering in Machine Learning
Tutorial for DBSCAN Clustering in Machine LearningTutorial for DBSCAN Clustering in Machine Learning
Tutorial for DBSCAN Clustering in Machine Learning
 
Python Code for Classification Supervised Machine Learning.pdf
Python Code for Classification Supervised Machine Learning.pdfPython Code for Classification Supervised Machine Learning.pdf
Python Code for Classification Supervised Machine Learning.pdf
 
Sql tutorial for select, where, order by, null, insert functions
Sql tutorial for select, where, order by, null, insert functionsSql tutorial for select, where, order by, null, insert functions
Sql tutorial for select, where, order by, null, insert functions
 
Kaler et al 2018 euphytica
Kaler et al 2018 euphyticaKaler et al 2018 euphytica
Kaler et al 2018 euphytica
 
Association mapping identifies loci for canopy coverage in diverse soybean ge...
Association mapping identifies loci for canopy coverage in diverse soybean ge...Association mapping identifies loci for canopy coverage in diverse soybean ge...
Association mapping identifies loci for canopy coverage in diverse soybean ge...
 
Genome wide association mapping
Genome wide association mappingGenome wide association mapping
Genome wide association mapping
 
Population genetics
Population geneticsPopulation genetics
Population genetics
 
Quantitative genetics
Quantitative geneticsQuantitative genetics
Quantitative genetics
 
Abiotic stresses in plant
Abiotic stresses in plantAbiotic stresses in plant
Abiotic stresses in plant
 
Multiple linear regression
Multiple linear regressionMultiple linear regression
Multiple linear regression
 
Correlation in Statistics
Correlation in StatisticsCorrelation in Statistics
Correlation in Statistics
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regression
 
Analysis of Variance (ANOVA)
Analysis of Variance (ANOVA)Analysis of Variance (ANOVA)
Analysis of Variance (ANOVA)
 
Population and sample mean
Population and sample meanPopulation and sample mean
Population and sample mean
 
Descriptive statistics and graphs
Descriptive statistics and graphsDescriptive statistics and graphs
Descriptive statistics and graphs
 
Hypothesis and Test
Hypothesis and TestHypothesis and Test
Hypothesis and Test
 
Normal and standard normal distribution
Normal and standard normal distributionNormal and standard normal distribution
Normal and standard normal distribution
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

R code descriptive statistics of phenotypic data by Avjinder Kaler

  • 2. Steps 1. Reading data into R—the data file includes a header and “NA” is a missing value. data_in <‐‐‐ read.table (file="example.dat", header= T, na.string="NA") 2. Getting access to the data frame (all variables will relate to this data frame). attach(data_in) 3. Overview of data (mean, median, maximum, minimum, first and third quartiles, number of missing values). summary(data_in) 4. Calculating means and standard deviations (2 indicates that the function is applied to all columns, na.rm=T means that missing values are removed). apply(data_in,2,mean,na.rm=T) apply(data_in,2,sd,na.rm=T) 5. Means or standard deviations can be calculated for data points grouped by a factor (e.g. year). aggregate(data_in,list(data$year),FUN=mean) aggregate(data_in,list(data$year),FUN=sd) 6. Frequencies for a single variable and across two variables. table(sex) table(wormy) table(wormy,sex)
  • 3. 7. Histogram. hist(FWEC) 8. xy scatter plots. plot(FWEC) 9. Box plot. boxplot(FWEC_sex,data=data_in, range=0) 10. Shapiro–Wilk’s tests to check normality of data distribution. shapiro.test(FWEC) 11. Checking data distribution with QQ plot—if data are normally distributed, the plotted data and the line are well aligned. qqplot(FWEC) qqline(FWEC) 12. Data transformation—log, square root, and cube root transformation. log_FWEC <‐‐‐ log(FWEC) sqrt_FWEC <‐‐‐ sqrt(FWEC+1) cbrt_FWEC <‐‐‐ (FWEC)^(1/3)
  • 4. 13. Box–Cox transformation. #CodetofindsuitablelambdaforYtothepowerlambda #download thelibrary(MASS) #seq(min value, max value, step) defines the range from which lambda is drawn boxcox(FWEC_factor(sex)+factor(birth_rearing_ type), lambda = seq(0,1.0,0.01) savePlot("boxcox","jpeg") lambda = "insert maximum lambda value in graph here" trans(FWEC) <- ((FWEC^lambda)-1)/lambda MASS library 14. Checking homogeneity of variances. #download library (Rcmdr) library(Rcmdr) #run the Leven’s test, specifying the vector of data y and group, the factor across which the variances are tested (e.g., year) leveneTest(y,group) 15. Fitting a linear model and ANOVA. #need to load the "car" package for Type III ANOVA library(car)
  • 5. lmod <- lm(cbrt_FWEC_factor(sex)) #Type I ANOVA anova(lm) #Type III ANOVA---Note that the first letter in the commandbelow has to be a capital "A" (ensure that you loaded the "car" package as shown above) Anova(lmod, type¼"III") 16. Addressing confounding of explanatory variables in a linear model. lmod1 <- lm(cbrt_FWEC_factor(sex)+factor(birth_ type)*factor(rearing_type)) lmod2 <-lm(cbrt_FWEC_factor(sex)+factor(birth_ rearing_type)) 17. Check the difference with an ANOVA. Anova(lmod1,type="III") Anova(lmod2,type="III") 18. Model comparison using logistic regression for binary data. logres <- glm(formula=wormy_status_factor(sex) + factor(birth_rearing_type), family = binomial (link="logit"))
  • 6. #producing an analysis-of-deviance table to test fixed effects anova(logres,test="Chisq") #produces the deviance of the model (the lower the better the fit) summary(glm(formula=wormy_status_factor(sex) + factor(birth_rearing_type), family = binomial (link="logit"))$deviance)) #the difference in deviance can be formally tested with a loglikelihood ratio test #install library(lme4) library(lme4) #comparing two nested models ("nested" means that one has one more factor than the other) logres1 <- lmer(wormy_status_factor(sex)), family = "binomial", method="Laplace") logres2 <- lmer(wormy_status_factor(sex) + factor (birth_rearing_type), family = "binomial", Method="Laplace") anova(logres1,logres2) #to assess the model, plot predicted probability against observed proportion #install library(languageR) library(languageR) plot.logistics.fit.fnc(logres1,logres2)
  • 7. 19. Model diagnostics. #the following produces plot of residual vs. fitted value, QQ plot, and scale-location plot of the previously tested model 1 (lmod1) plot(lmod1) #assessing a logit model for binary data by plotting the predicted probability against observed proportions #download library(languageR) library(languageR) plot.logistic.fit.fnc(logres1,data_in) 20. Extracting residuals and writing them to a file—assuming lmod2 is the model of choice. res_lmod2 <-residuals(lmod2) write.table(res_lmod2,file¼"res_FWEC")