Sei sulla pagina 1di 4

18/04/2016

PrepareDataforLinearMixedEffectsModelsMATLAB&Simulink

PrepareDataforLinearMixedEffectsModels
TablesandDatasetArrays
Tofitalinearmixedeffectsmodel,youmuststoreyourdatainatableordatasetarray.Inyourtableordataset
array,youmusthaveacolumnforeachvariableincludingtheresponsevariable.Morespecifically,thetableor
datasetarray,saytbl,mustcontainthefollowing:

Aresponsevariabley

PredictivevariablesXjwhichcanbecontinuousorgroupingvariables

Groupingvariablesg1,g2,...,gR,

wherethegroupingvariablesinXjandgrcanbecategorical,logical,characterarrays,oracellarraysof
strings,r=1,2,...,R.
Youmustorganizeyourdatasothateachrowrepresentsanobservation.Andeachrowshouldcontainthe
valueofvariablesandthelevelsofgroupingvariablescorrespondingtothatobservation.Forexample,ifyou
havedatafromanexperimentwithfourtreatmentoptions,onfivedifferenttypesofindividualschosen
randomlyfromapopulationofindividuals(blocks),thetableordatasetarraymustlooklikethis.
Block

Treatment

Response

y11

y12

y13

y14

...

...

...

y51

y52

y53

y54

Now,considerasplitplotexperiment,wheretheeffectoffourdifferenttypesoffertilizersontheyieldof
tomatoplantsisstudied.Thesoilwherethetomatoplantsareplantedisdividedintothreeblocksbasedonthe
soiltype:sandy,silty,andloamy.Eachblockisdividedintofiveplots,wherefivetypesoftomatoplants,
(cherry,heirloom,grape,vine,andplum)arerandomlyassignedtotheseplots.Then,thetomatoplantsinthe
plotsaredividedintosubplots,whereeachsubplotistreatedbyoneofthefourfertilizers.Thedatafromthis
experimentlookslike:
Soil

Tomato

Fertilizer

Yield

'Sandy'

'Plum'

104

'Sandy'

'Plum'

136

'Sandy'

'Plum'

158

'Sandy'

'Plum'

174

'Sandy'

'Cherry'

57

'Sandy'

'Cherry'

86

...

...

...

...

'Sandy'

'Vine'

99

'Sandy'

'Vine'

117

http://www.mathworks.com/help/stats/preparedataforlinearmixedeffectsmodels.html

1/4

18/04/2016

PrepareDataforLinearMixedEffectsModelsMATLAB&Simulink

'Silty'

'Plum'

120

'Silty'

'Plum'

115

...

...

...

...

'Loamy'

'Vine'

111

'Loamy'

'Vine'

105

Youmustspecifythemodelyouwanttofitusingtheformulainputargumenttofitlme.
Ingeneral,aformulaformodelspecificationisastringoftheterm'y~terms'.Forlinearmixedeffects
models,thisformulaisintheform'y~fixed+(random1|grouping1)+...+(randomR|groupingR)',
wherefixedcontainsthefixedeffectstermsandrandom1,...,randomRcontaintherandomeffectsterms.
Forexample,forthepreviousfertilizerexperiment,considerthefollowingmixedeffectsmodel

yimjk = 0 +

m=2

[ ]
[ ]
1mI F im +
2jI T ij + b0k Sk + b0jk (S T ) jk + imjk ,
j=2

wherei=1,2,...,60,theindexmcorrespondstothefertilizertypes,jcorrespondstothetomatotypes,andk
=1,2,3correspondstotheblocks(soil).Skrepresentsthekthsoiltype,andI[F]imisthedummyvariable
representinglevelmofthefertilizer.Similarly,I[T]ijisthedummyvariablerepresentingtheleveljofthetomato
type.
Youcanfitthismodelusingtheformula'Yield~1+Fertilizer+Tomato+(1|Soil)+
(1|Soil:Tomato)'.
Fordetailedinformationonhowtospecifyyourmodelusingformula,seeRelationshipBetweenFormulaand
DesignMatrices.

DesignMatrices
Ifyoucannoteasilydescribeyourmodelusingaformula,youcancreatedesignmatricestodefinethefixed
andrandomeffects,andfitthemodelusingfitlmematrix(X,y,Z,G).Youmustcreateyourdesignmatrices
asfollows.
FixedeffectsandrandomeffectsdesignmatricesXandZ:

Enteracolumnof1sfortheinterceptusingones(n,1),wherenisthetotalnumberofobservations.

IfX1isacontinuousvariable,thenenterX1asitisinaseparatecolumn.

IfX1isacategoricalvariablewithmlevels,thentheremustbem1dummyvariablesform1levelsof
X1inX.
Forexample,consideranexperimentwhereyouwanttostudytheimpactofqualityofrawmaterialsfrom
fourdifferentprovidersontheproductivityofaproductionline.Ifyoufitalinearmixedeffectsmodelwith
interceptandproviderasthefixedeffectsterms,interceptistherandomeffectsterm,andyouuse
referencecontrastscoding,thenyoumustconstructyourfixedandrandomeffectsdesignmatricesas
follows.
D=dummyvar(provider);%Createdummyvariables
X=[ones(n,1)D(:,2)D(:,3)D(:,4)];
Z=[ones(n,1)];
Becausereferencecontrastcodingusesthefirstproviderasthereference,andthemodelhasanintercept,
youmustusethedummyvariablesforonlythelastthreeproviders.

IfthereisaninteractiontermofpredictorvariablesX1andX2,thenyoumustenteracolumnthatyouform
byelementwiseproductofthevectorsX1andX2.
Forexample,ifyouwanttofitamodel,wherethereisanintercept,acontinuoustreatmentfactor,a
continuoustimefactor,andtheirinteractionasthefixedeffectsinalongitudinalstudy,andtimeisthe
randomeffectsterm,thenyourfixedandrandomeffectsdesignmatricesshouldlooklike

http://www.mathworks.com/help/stats/preparedataforlinearmixedeffectsmodels.html

2/4

18/04/2016

PrepareDataforLinearMixedEffectsModelsMATLAB&Simulink

X=[ones(n,1),treatment,time,treatment.*time];
y=response;
Z=[time];
GroupingvariablesG:
Thereisonecolumnforeachgroupingvariableandacolumnofelementwiseproductofthegroupingvariables
incaseofanesting.
Forexample,ifyouwanttogroupplots(plot)withinblocks(block),thenyoumustaddacolumnof
elementwiseproductofplotbyblock.Morespecifically,ifyouwanttofitamodelwherethereisintercept
andacontinuoustreatmentfactorasthefixedeffectsinasplitblockexperiment,andtheinterceptand
treatmentaregroupedbytheplotsnestedwithinblocks,thenthedesignmatricesshouldlooklikethis.
X=[ones(n,1),treatment];
y=response;
Z=[ones(n,1),treatment];
G=[block.*plot];
Supposeintheearlierqualityofrawmaterialsexample,therawmaterialsarriveinbulks,andthebulksare
nestedwithinproviders.Ifyouwanttofitalinearmixedeffectsmodel,whereinterceptisgroupedbythebulks
withinproviders,thenyourdesignmatricesshouldlooklikethis.
D=dummyvar(provider);
X=[ones(n,1)D(:,2)D(:,3)D(:,4)];
y=response;
Z=ones(n,1);
G=[provider.*bulks];
Intheearlierlongitudinalstudyexample,ifyouwanttoaddrandomeffectsforinterceptandtimegroupedby
subjectsthatparticipatedinthestudy,thenyourdesignmatricesshouldlooklike
X=[ones(n,1),treatment,time,treatment.*time];
y=response;
Z=[ones(n,1),time];
G=subject;

RelationofMatrixFormtoTablesandDatasetArrays
fitlme(tbl,formula)andfitlmematrix(X,y,Z,G)areequivalentinfunctionality,suchthat

yisthenby1responsevector.

Xisannbypfixedeffectsdesignmatrix.fitlmeconstructsthisfromtheexpressionfixedinformula.

ZisanRby1cellarraywithZ{r}beingannbyq(r)randomeffectsdesignmatrixconstructedfromtherth
expressioninrandominformula,r=1,2,...,R.

GisanRby1cellarraywithG{r}beingannby1groupingvariable,gr,informulawithM(r)levelsor
groups.

Forexample,iftblisatableordatasetarraycontainingtheresponsevariabley,thecontinuousvariablesX1
andX2,andthegroupingvariableg,thentofitalinearmixedeffectsmodelthatcorrespondstotheformula
expression'y~X1+X2+(X1*X2|g)'usingfitlmematrix(X,y,Z,G)theinputargumentsmustcorrespond
tothefollowing:
y=tbl.y
X=[ones(n,1),tbl.X1,tbl.X2]
Z=[ones(n,1),tbl.X1,tbl.X2,tbl.X1.*tbl.X2]
G=tbl.g
http://www.mathworks.com/help/stats/preparedataforlinearmixedeffectsmodels.html

3/4

18/04/2016

PrepareDataforLinearMixedEffectsModelsMATLAB&Simulink

SeeAlso
fitlme|fitlmematrix|LinearMixedModel

MoreAbout

LinearMixedEffectsModels

RelationshipBetweenFormulaandDesignMatrices

http://www.mathworks.com/help/stats/preparedataforlinearmixedeffectsmodels.html

4/4

Potrebbero piacerti anche