Createsampledatamatrixywithcolumnsthatareconstants,plusrandomnormaldisturbanceswithmean0andstandarddeviation1.
y=meshgrid(1:5);rngdefault;%Forreproducibilityy=y+normrnd(0,1,5,5)y=5×51.53770.69231.65013.79505.67152.83391.56646.03493.87593.7925-1.25882.34263.72545.48975.71721.86225.57842.93695.40906.63021.31884.76943.71475.41725.4889Performone-wayANOVA.
p=anova1(y)
p=0.0023TheANOVAtableshowsthebetween-groupsvariation(Columns)andwithin-groupsvariation(Error).SSisthesumofsquares,anddfisthedegreesoffreedom.Thetotaldegreesoffreedomistotalnumberofobservationsminusone,whichis25-1=24.Thebetween-groupsdegreesoffreedomisnumberofgroupsminusone,whichis5-1=4.Thewithin-groupsdegreesoffreedomistotaldegreesoffreedomminusthebetweengroupsdegreesoffreedom,whichis24-4=20.
MSisthemeansquarederror,whichisSS/dfforeachsourceofvariation.TheF-statisticistheratioofthemeansquarederrors(13.4309/2.2204).Thep-valueistheprobabilitythattheteststatisticcantakeavaluegreaterthanthevalueofthecomputedteststatistic,i.e.,P(F>6.05).Thesmallp-valueof0.0023indicatesthatdifferencesbetweencolumnmeansaresignificant.
Inputthesampledata.
Testthenullhypothesisthatthesteelbeamsareequalinstrengthtothebeamsmadeofthetwomoreexpensivealloys.TurnthefiguredisplayoffandreturntheANOVAresultsinacellarray.
MSisthemeansquarederror,whichisSS/dfforeachsourceofvariation.TheF-statisticistheratioofthemeansquarederrors.Thep-valueistheprobabilitythattheteststatisticcantakeavaluegreaterthanorequaltothevalueoftheteststatistic.Thep-valueof1.5264e-04suggestsrejectionofthenullhypothesis.
YoucanretrievethevaluesintheANOVAtablebyindexingintothecellarray.SavetheF-statisticvalueandthep-valueinthenewvariablesFstatandpvalue.
[~,~,stats]=anova1(strength,alloy);
Thesmallp-valueof0.0002suggeststhatthestrengthofthebeamsisnotthesame.
Performamultiplecomparisonofthemeanstrengthofthebeams.
[c,~,~,gnames]=multcompare(stats);
Inthefigure,thebluebarrepresentsthecomparisonintervalformeanmaterialstrengthforsteel.Theredbarsrepresentthecomparisonintervalsforthemeanmaterialstrengthforalloy1andalloy2.Neitheroftheredbarsoverlapswiththebluebar,whichindicatesthatthemeanmaterialstrengthforsteelissignificantlydifferentfromthatofalloy1andalloy2.Youcanconfirmthesignificantdifferencebyclickingthebarsthatrepresentalloy1and2.
Displaythemultiplecomparisonresultsandthecorrespondinggroupnamesinatable.
Thefirsttworowsshowthatbothcomparisonsinvolvingthefirstgroup(steel)haveconfidenceintervalsthatdonotincludezero.Becausethecorrespondingp-values(1.6831e-04and0.0040,respectively)aresmall,thosedifferencesaresignificant.
Thethirdrowshowsthatthedifferencesinstrengthbetweenthetwoalloysisnotsignificant.A95%confidenceintervalforthedifferenceis[-5.6,1.6],soyoucannotrejectthehypothesisthatthetruedifferenceiszero.Thecorrespondingp-valueof0.3560inthesixthcolumnconfirmsthisresult.
Sampledata,specifiedasavectorormatrix.
Note
anova1ignoresanyNaNvaluesiny.Also,ifgroupcontainsemptyorNaNvalues,anova1ignoresthecorrespondingobservationsiny.Theanova1functionperformsbalancedANOVAifeachgrouphasthesamenumberofobservationsafterthefunctiondisregardsemptyorNaNvalues.Otherwise,anova1performsunbalancedANOVA.
DataTypes:single|double
Groupingvariablecontaininggroupnames,specifiedasanumericvector,logicalvector,categoricalvector,characterarray,stringarray,orcellarrayofcharactervectors.
IfgroupcontainsemptyorNaNvalues,anova1ignoresthecorrespondingobservationsiny.
DataTypes:single|double|logical|categorical|char|string|cell
p-valuefortheF-test,returnedasascalarvalue.p-valueistheprobabilitythattheF-statisticcantakeavaluelargerthanthecomputedtest-statisticvalue.anova1teststhenullhypothesisthatallgroupmeansareequaltoeachotheragainstthealternativehypothesisthatatleastonegroupmeanisdifferentfromtheothers.Thefunctionderivesthep-valuefromthecdfoftheF-distribution.
Ap-valuethatissmallerthanthesignificancelevelindicatesthatatleastoneofthesamplemeansissignificantlydifferentfromtheothers.Commonsignificancelevelsare0.05or0.01.
ANOVAtable,returnedasacellarray.tblhassixcolumns.
TherowsoftheANOVAtableshowthevariabilityinthedatathatisdividedbythesource.
Boxplotsincludenotchesforthecomparisonofthemedianvalues.Twomediansaresignificantlydifferentatthe5%significanceleveliftheirintervals,representedbynotches,donotoverlap.ThistestisdifferentfromtheF-testthatANOVAperforms;however,largedifferencesinthecenterlinesoftheboxescorrespondtoalargeF-statisticvalueandcorrespondinglyasmallp-value.Theextremesofthenotchescorrespondtoq2–1.57(q3–q1)/sqrt(n)andq2+1.57(q3–q1)/sqrt(n),wherenisthenumberofobservationswithoutanyNaNvalues.Insomecases,notchescanextendoutsidetheboxes.
[1]Hogg,R.V.,andJ.Ledolter.EngineeringStatistics.NewYork:MacMillan,1987.
IntroducedbeforeR2006a
Youcanalsoselectawebsitefromthefollowinglist
HowtoGetBestSitePerformance
SelecttheChinasite(inChineseorEnglish)forbestsiteperformance.OtherMathWorkscountrysitesarenotoptimizedforvisitsfromyourlocation.