Two-Way ANOVA in R – Step-by-Step Tutorial - statdoe

文章推薦指數: 80 %
投票人數:10人

Analysis of variance for two factors – Two-Way ANOVA ... For the ANOVA table, we need to have the independent variables in the data file defined ... Tutorials 15 Jun 2021 Two-WayANOVAinR–Step-by-StepTutorial ANOVA,Tukey’stest,summarisedtable,compactletterdisplay,barplot,scatterplot RosaneRech Loadingtheappropriatelibraries Loadingandcheckingthedata Creatingasimpleplotfordatavisualisation Analysisofvariancefortwofactors–Two-WayANOVA Creatingatablewithfactors,meansandstandarddeviation ComparingmeansbyTukey’stest Compactletterdisplaytoindicatesignificantdifferences Plotsforthefinalpresentationoftheresults. Barplots Scatterplot Relatedvideotutorials Inthistutorial,wearegoingtoanalysetheresultsofatwo-factorfactorialdesignusinganalysisofvariance(ANOVA). Youaregoingtolearnhowto: createasimpleplotforthedatavisualisation performanalysisofvarianceandTukey’stest obtainthecompactletterdisplay(cld)toindicatesignificantdifferences buildatablewiththesummariseddata:mean,standarddeviationandcld Attheendofthetutorialyouwillalsofindexamplesofbarplotsandscatterplotsforthefinalpresentationoftheresults. Thedata1presentstheresultsofanexperimentconductedtostudytheinfluenceoftheoperatingtemperature(100˚C,125˚Cand150˚C)andthreefaceplateglasstypes(A,BandC)inthelightoutputofanoscilloscopetube. Toreproducetheresults,youcandownloadthefileGTL.csv. Loadingtheappropriatelibraries Wearegoingtostartbyloadingtheappropriatelibraries,thereadrtoloadthedatafromacsvfile,theggplot2fortheplots,thedplyrforbuildingatablewiththesummariseddata(meanandstandarddeviation)andthemultcompViewtousethecompactletterdisplaytoindicatesignificantdifferencesinTukey’stest. #loadingtheappropriatelibraries library(readr) library(ggplot2) library(multcompView) library(dplyr) Loadingandcheckingthedata Thefirststepoftheanalysisistoloadthedatafile.Wewillusetheheadfunctiontoseethefirstrowsandthestrfunctionforthefilestructure. #loadingandcheckingthedata GTL ##1A100580 ##2A100568 ##3A100570 ##4B100550 ##5B100530 ##6B100579 str(GTL) ##spec_tbl_df[27×3](S3:spec_tbl_df/tbl_df/tbl/data.frame) ##$Glass:chr[1:27]"A""A""A""B"... ##$Temp:num[1:27]100100100100100100100100100125... ##$Light:num[1:27]5805685705505305795465755991090... ##-attr(*,"spec")= ##..cols( ##..Glass=col_character(), ##..Temp=col_double(), ##..Light=col_double() ##..) Wecanseethatwehave27observationsofthreevariables(columns):Glass,Temp(factors)andLight(responsevariable).Glassisdefinedasacharacter,andTempandLightarenumericvariables. Creatingasimpleplotfordatavisualisation Itisalwaysagoodideatovisualisethebehaviouroftheexperimentaldatabeforedivingintotheanalysis.Wewillusethefunctionqplotfromtheggplot2library. #buildingasimpleplotfordatavisualisation qplot(x=Temp,y=Light,geom="point",data=GTL)+ facet_grid(.~Glass,labeller=label_both) WecanseethatthelightoutputincreaseswiththetemperatureforfaceplateglasstypesAandB;however,faceplateglasstypeChasamaximumat125ºC.Theplotsindicatethatwewillprobablyhaveatleastthetemperatureandtheinteractionbetweentemperatureandglassassignificant. Analysisofvariancefortwofactors–Two-WayANOVA FortheANOVAtable,weneedtohavetheindependentvariablesinthedatafiledefinedasfactors.ForGlass,whichisoriginallydefinedasacharacter,wewillconvertitintoafactor.Fortemperature,whichisanumericvariable,wewillduplicateit,creatinganewvariable,Temp_Factor,definedasafactor.Wearenotgoingtoconvertitbecauseitispossiblewearegoingtoneeditasnumericforfurtheranalysisorplots. #creatingavariableasfactorfortheANOVA GTL$GlassF) ##Glass215086575432206.43.89e-13*** ##Temp_Factor219703359851672695.3<2e-16*** ##Glass:Temp_Factor429055272638198.71.25e-14*** ##Residuals186579366 ##--- ##Signif.codes:0'***'0.001'**'0.01'*'0.05'.'0.1''1 Consideringthelevelofsignificance0.05,theANOVAtableshowsthataresignificantthetemperature,thefaceplateglasstype,andtheinteractionbetweenthem.Thisresultisexpectedconsideringthepreviousvisualisationofthedataperformedintheprevioussection. Creatingatablewithfactors,meansandstandarddeviation Nowwearegoingtobuildatablewiththemeanandthestandarddeviationofthelightoutputforeachtreatment(combinationoffaceplateglassandtemperature).Whencreatingdetable,wearegoingtoarrangethemeansinadecreasingorder,whichwillbenecessaryforaddingthesuperscriptlettersfromTukey’stest.Thiscodeusesthedplyrpackage. #tablewithfactors,meansandstandarddeviation data_summary% summarise(mean=mean(Light),sd=sd(Light))%>% arrange(desc(mean)) print(data_summary) ###Atibble:9x4 ###Groups:Glass[3] ##GlassTempmeansd ## ##1A15013866 ##2B150131314.5 ##3A1251087.2.52 ##4C1251055.10.6 ##5B125103535 ##6C150887.18.6 ##7C100573.26.5 ##8A100573.6.43 ##9B10055324.6 ComparingmeansbyTukey’stest ThemeanscomparisonbyTukey’stestcanberunontheobjectresultingfromtheanalysisofvariance.Theresult(below)isanextensivetablewithallpairwisecomparisonsandthe\(p\)-valueforeachoneofthem.However,thisdatacanbetrickytointerpret,anditisusualtouseletterstoindicatesignificantdifferencesamongthemeans. #Tukey'stest tukey ##1A15013866a ##2B150131314.5b ##3A1251087.2.52c ##4C1251055.10.6c ##5B125103535c ##6C150887.18.6d ##7C100573.26.5e ##8A100573.6.43e ##9B10055324.6e Thetablecanbeexportedasacsvfileusingthewrite.csv()function. write.csv("GTL_summary.csv") Iftheideaistopresentthefinaldataasatable(asshownbelow),theanalysiscanendhere.However,itisalwaysinterestingtopresenttheresultsinavisualform.Sothenextsectionsshowcodestobuildbarandlineplotssuitableforthepresentationofthisparticularresult. Lightoutputofanoscilloscopetubewiththetemperatureandfaceplateglasstype. Temperature GlassType 100ºC 125ºC 150ºC A 573±06e 1087±3c 1386±06a B 553±25e 1035±35c 1313±15b C 573±27e 1055±11c 887±19d Plotsforthefinalpresentationoftheresults. Thissectionshowsexamplesofbarandscatterplotssuitableforpresentations,postersandwrittenreports.Theplotsshowthemean,thestandarddeviationandthecompactletterdisplayforeachtreatment. Barplots Thecolouredbarplotpresentsthefaceplateglasslabelsatthebaseofthecolumns,makingitpossibletointerprettheresultsevenwhenprintedingrey-scaleorbyacolourblindindividual.Inthiscaseofthegrey-scalebarplot,theglasslabelsatthebaseofthecolumnsarenotnecessarysincetheshadesofgreyareclearenoughtoidentifytheresults. Astep-by-steptutorialonhowtobuildtheseplotscanbefoundonbuildingbarplotsfortwofactorsinR. Scatterplot Anotherwayofconveyingthefinalresultsforthisparticularexampleisbyusingscatterplots,asoneofthevariables,thetemperature,isacontinuousnumericvariable.Thedifferentfaceplateglasstypescanbeidentifiedbythemarkershapeandlinetypeinbothplots,andalsobythecolourinthecolouredplot.Aslighttransparencywasappliedonthemarkersfortheerrorbarstobevisible. Astep-by-steptutorialonhowtobuildtheseplotscanbefoundonbuildingscatterplotsfortwofactorsinR. Relatedvideotutorials Two-WayANOVARtutorial1–AnalysisofVariance Two-WayANOVARtutorial2–Tukey’sTest Datasource:Designandanalysisofexperiments/DouglasC.Montgomery.—Eighthedition↩︎ CustomisingtheCompactLetterDisplayPosition BarplotforTwoFactorsinR–Step-by-StepTutorial AboutPostAuthor RosaneRech 13Responses HiRosane,yourexplanationwasveryhelpful. Ihaveaquestion.IwanttovisualizemydatabutIcouldn’tgetthrough.Iwanttocompareacharacterandavariableinaonefactorexperiment. Iwanttocompareantherextrusion(numeric)inwheatgenotypes(characters).HowdoIsolvethisissue. Thankyou Agetha HiAgetha,youcanalwaysusecategoricalfactorsinyourhorizontalaxis. HiRosane,thankyousomuchforit.Itwasreallyhelpful.Canyoukindlytellmehowtomakethetableyouattachedherewiththemean+sd+lettersallinasinglecolumn.Doesithaveacode? Thanksinadvance.. UnfortunatlyIdon’thaveacodeforit. IhaveanotherquestionMytukeyresultsshowingletterslikea,a,ab,bc,cd.Isthereanywaytostoprepeatingthelettersandjusttokeepitassingleletterlike,a,b,c,d? Thankyou. HiSilmoon,ifthosearetheresultsofyourTukey’stest,youcannotmanuallychangethem. HiRosane,Ihopeyouareverywell. Ihaveaquestion.WhywhenIrunthetukeywithmydata:tukey.cld% arrange(desc(mean)) print(data_summary) #TukeytesttostudyeachpairoftreatmentERROR!!!# TukeyHSD(aov(logExpr~Treatment,data=my_data)) #Ineedtogroupthetreatmentsthatarenot #differenteachothertogether# library(multcompView) generate_label_df% arrange(desc(mean)) print(data_summary) #Tukey’stest tukey



請為這篇文章評分?