控制如何显示效果图中的交互术语

时间:2015-07-20 21:00:47

标签: r plot effects interaction mixed-models

我试图使用效果图从线性混合效果模型绘制交互项。见下面的例子:

library(nlme)
fitA <- lme(PEE ~ Pupper*max_depth,
  random=~1 + Pupper|ref, data=m4,
  cor=corAR1(), method="ML")

Pupper是一个连续变量,max_depth是一个因子变量(5个级别 - 400,500,600,700,800)。

当我在具有交互项的效果图中绘制模型输出时,我能够显示PEEPupper之间的关系如何根据max_depth的不同因素级别而变化:

library(effects)
plot(effect("Pupper*max_depth",fitA),
  xlab=expression(paste("d"[-5]," P"[upper]," (m"^" -1",")")),
  ylab=expression(paste("PEE rate (h"^" -1",")")),      
        factor.names=FALSE, layout=c(5,1), alternating=FALSE, 
     main="A", ticks.x=list(Pupper=list(at=seq(0.4,1.0,0.2))), 
   ##layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
        rotx=45, more=FALSE, grid=FALSE, lwd=1)

plot1

然而,有时当我绘制一个类似的模型时,效果图将改变我希望交互的显示方式(见下文)。在下面的效果图中,情节决定&#39;显示PEEmax_depth之间的关系以及它如何根据连续变量Pupper(代码中标记为Plower和&#39; k150&#)的任意划分而变化39;在下图中显示:

fitB <- lme(PEE ~ Plower*max_depth,
   random=~1 + Plower|ref, data=m4, 
   cor=corAR1(), method="ML")

plot(effect("Plower*max_depth",fitB),
xlab=expression(paste("d"[-5]," P"[lower]," (m"^" -1",")")), 
ylab=expression(paste("PEE rate (h"^" -1",")")),        
factor.names=FALSE, layout=c(5,1), alternating=FALSE,
  main="A", ticks.x=list(Plower=list(at=seq(0.4,1.0,0.2))), 
 ##layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
rotx=45, more=FALSE, grid=FALSE, lwd=1)

plot2

但是,我对因子max_depth如何影响PEEPlower之间的关系感兴趣(类似于上面的第一个图)。我无法弄清楚为什么效果功能以两种不同的方式显示相同的交互项。我很想知道如何控制交互项在效果图中的表示方式,因为这个问题不断突然出现。

以下是我的数据集的一个子集:

structure(list(ref = c("2012-3corrige", "2011-28", "2011-26", 
"2011-21", "2011-21", "2013-7", "2012-1corrige", "2012-6corrige", 
"2013-4", "2011-21", "2013-10", "2013-4", "2013-13", "2011-26", 
"2013-11", "2012-3corrige", "2013-1", "2012-14corrige", "2013-1", 
"2011-27", "2012-6corrige", "2011-18", "2011-26", "2010-18", 
"2012-14corrige", "2011-21", "2013-6", "2013-11", "2011-27", 
"2011-18", "2012-16corrige", "2013-5", "2013-13", "2011-21", 
"2012-14corrige", "2013-5", "2013-18", "2012-16corrige", "2011-28", 
"2010-18", "2011-21", "2013-2", "2012-2corrige", "2013-4", "2013-5", 
"2013-11", "2011-21", "2013-6", "2011-28", "2013-6", "2010-18", 
"2011-21", "2013-18", "2011-16", "2012-11corrige", "2011-28", 
"2011-27", "2012-3corrige", "2012-2corrige", "2013-3", "2012-1corrige", 
"2012-14corrige", "2012-14corrige", "2013-10", "2012-6corrige", 
"2010-18", "2012-11corrige", "2013-7", "2013-2", "2012-16corrige", 
"2013-1", "2013-18", "2012-16corrige", "2013-6", "2012-4corrige", 
"2013-4", "2013-10", "2013-3", "2013-2", "2011-16", "2012-1corrige", 
"2011-21", "2011-21", "2013-18", "2013-3", "2011-26", "2010-18", 
"2013-13", "2012-6corrige", "2013-3", "2012-16corrige", "2012-15corrige", 
"2011-28", "2012-6corrige", "2012-6corrige", "2012-11corrige", 
"2013-1", "2013-11", "2012-11corrige", "2013-6"), Pupper = c(0.861958207287982, 
0.824829924556841, 0.958739109455748, 0.935401831656677, 0.955566680038604, 
0.948368978826279, 0.745071680369673, 0.827539122942233, 0.726448658429027, 
0.943103302931338, 0.858445846226439, 0.784802718309937, 0.881010495586365, 
0.911770168408684, 0.90971638692581, 1.02155421458351, 0.851778844536538, 
1.1553118943962, 0.887452083213511, 0.8218157295485, 0.871777265131409, 
0.829892474962871, 1.01579427707254, 0.715539162683171, 1.12624787680155, 
0.713105471394893, 0.802478037082636, 0.773243110590944, 0.762028205952159, 
0.785089166910358, 0.844285844170484, 0.887514023676371, 0.870367623478723, 
0.820303824472643, 0.636099278958915, 0.953776661488422, 0.816485694234068, 
0.861493535070196, 0.787945463425822, 0.918041865421543, 0.877275056321815, 
0.624152855209897, 0.971197595182818, 0.769613695304339, 0.941443459091764, 
0.929070549770906, 1.031203743205, 0.692597025693873, 0.846978945035432, 
0.72446749179426, 0.541564092852052, 0.744921803502444, 0.917786983273715, 
0.702051561892398, 0.975310403563878, 0.808819367281032, 0.858040403089116, 
0.741495941398947, 0.698143566897239, 0.979366380200314, 0.992046903013047, 
0.995331870590213, 0.804437082665078, 0.8307554779262, 0.878549524310762, 
0.654725061889849, 0.93024953667308, 0.654611447094126, 0.689696271315618, 
0.77302453480932, 0.916283427766758, 0.894114399839305, 0.840205756601608, 
0.767235548359607, 0.831544386468135, 0.685089269122402, 0.860269828471148, 
0.895228365651283, 0.785946885397904, 0.812567650516969, 0.797256286689962, 
0.800979891549511, 0.684467773772683, 0.846228645225391, 0.801015938251751, 
0.964375424821682, 0.783654311543043, 0.951249150678552, 0.847095453102345, 
0.782862048298847, 0.897798965949478, 0.79591714811698, 0.954852044385237, 
0.885914708711347, 0.789575506205708, 1.10814372714012, 0.875651148193922, 
0.851523408695002, 0.963324355206144, 0.795071091161036), Plower = c(0.705132769215998, 
0.667302197075824, 0.629978835623335, 0.632452896796802, 0.641619045851976, 
0.634150350206216, 0.521875889886134, 0.69048678481199, 0.620155894379255, 
0.72673011955379, 0.644805071164551, 0.691418831100224, 0.561990510002912, 
0.702502669034076, 0.5885329988032, 1.06019049650942, 0.610499795249761, 
0.863589408611907, 0.671649710290516, 0.7008237216939, 0.613070958372683, 
0.52121652570373, 0.743727100487806, 0.619214556245787, 1.0217109832694, 
0.653199816289013, 0.653255947797901, 0.629436452185155, 0.621227279933305, 
0.581484689776476, 0.605084016204913, 0.670828674932066, 0.694246594037978, 
0.732994239783339, 0.728155423409921, 0.657673931367209, 0.681945582710071, 
0.656113353702447, 0.55299186250794, 0.589741939797023, 0.760512984767519, 
0.550684422635445, 0.888934443277143, 0.615143614667881, 0.736486026117717, 
0.616589579139919, 0.640405340389975, 0.618517043688639, 0.612475849864031, 
0.681245183469212, 0.642477842246546, 0.683125578173995, 0.636702442275825, 
0.568741300299764, 0.681781639762194, 0.58956858049858, 0.697614984548545, 
0.773372843818268, 0.599073358520381, 0.653548263966276, 0.846172099647715, 
0.946825538132655, 0.635504629303462, 0.61980005655224, 0.594418483337567, 
0.610786378368084, 0.809715477703094, 0.596886365511337, 0.601414998150196, 
0.680138336678131, 0.672368946338244, 0.693205917779446, 0.736742863266092, 
0.636678882954351, 0.611664395999418, 0.630585706572337, 0.6554630468205, 
0.617362130357864, 0.615793526002561, 0.688748462389895, 0.733587834625896, 
0.715468455706547, 0.695921451506322, 0.649384323802169, 0.654685675216232, 
0.675344356606317, 0.617759392212426, 0.620895052860519, 0.652138022200822, 
0.638494322605926, 0.798451637031189, 0.884450865414997, 0.895823643358446, 
0.661857655055493, 0.743487278528243, 0.88302132854573, 0.660494764046872, 
0.638155299450374, 0.515272975866573, 0.636047692132176), PEE = c(1.49625537031302, 
0.579708304983786, 1.09755665230733, 0.79999598579792, 0.366971323405136, 
0.534519852186464, 0.892172302701565, 0.764300080784289, 0.162161584516302, 
1.05547854644252, 1.75994722974226, 0.502090519778478, 0.813556191910923, 
0.72071830101183, 0.124737712452804, 0.24096278221797, 2.11763754191128, 
0.0970872140009704, 0.214668546888839, 0.997227687637828, 0.449413221941473, 
0.445533213208998, 0.719422276286327, 0.311417756794472, 0.0799998795735146, 
0.836011454943211, 0.217381544231536, 0.131863894834852, 1.1881086717854, 
0.562478645146688, 2.13755423670725, 0.260855398500945, 0.769228719926564, 
0.792077729637109, 0.46631964160662, 0.727219913477435, 0.234599414042217, 
1.24135448496734, 1.73912823566756, 0.437157161658986, 1.18491673172712, 
1.57894265236866, 0.325374033367569, 0.133629870488068, 0.260855348600893, 
0.279711624960117, 1.04650548272943, 1.13790951622142, 0.512819441159373, 
2.51301278595252, 0.948078086013639, 0.183485515251287, 0.521708195407091, 
0.834371581292816, 0.907354231373586, 0.263735732207326, 0.94736384877553, 
0.865382874911045, 0.162378076290205, 1.80685106084338, 1.07131194190618, 
1.20567188480079, 1.01009693910426, 0.352933736835024, 0.315767760495837, 
0.901500577761704, 1.08481956174672, 0.553504151294972, 1.81542854475215, 
2.23136249824668, 0.14018646847029, 0.58250584995577, 1.74754206600435, 
0.404021461283339, 1.0507621403718, 3.1578487818322, 1.23592560921063, 
0.428569941841892, 2.59927399028359, 0.462953155929056, 1.60334686111772, 
0.610996390428844, 0.93749693604517, 0.374210416022193, 0.596024133599949, 
1.07142175386991, 0.233917466116807, 0.773637607411201, 0.733915433670645, 
0.693195231932592, 0.699678270730694, 0.75104196328333, 1.1707299559812, 
0.376558572007052, 1.5725384212365, 0.17424722659426, 0.481925512179189, 
0.127383975172354, 0.449990814000021, 0.828701950628209), max_depth = structure(c(5L, 
1L, 5L, 5L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 5L, 3L, 3L, 3L, 4L, 5L, 
1L, 5L, 1L, 5L, 2L, 4L, 3L, 3L, 3L, 3L, 2L, 4L, 2L, 2L, 5L, 2L, 
2L, 4L, 5L, 4L, 2L, 1L, 5L, 2L, 1L, 4L, 4L, 4L, 3L, 4L, 1L, 2L, 
2L, 5L, 5L, 4L, 1L, 3L, 1L, 3L, 2L, 4L, 1L, 5L, 1L, 5L, 4L, 4L, 
1L, 2L, 4L, 1L, 1L, 4L, 4L, 2L, 2L, 2L, 4L, 2L, 5L, 1L, 2L, 1L, 
3L, 4L, 3L, 1L, 5L, 1L, 4L, 4L, 3L, 3L, 3L, 1L, 3L, 1L, 1L, 4L, 
1L, 4L, 2L), .Label = c("400", "500", "600", "700", "800"), class = "factor"), 
    fangle = structure(c(2L, 3L, 1L, 4L, 3L, 4L, 3L, 3L, 3L, 
    3L, 2L, 4L, 2L, 4L, 2L, 4L, 4L, 4L, 4L, 1L, 4L, 2L, 2L, 4L, 
    3L, 4L, 4L, 2L, 2L, 2L, 4L, 3L, 3L, 4L, 1L, 3L, 4L, 2L, 3L, 
    3L, 4L, 3L, 2L, 3L, 3L, 3L, 2L, 1L, 2L, 2L, 3L, 3L, 2L, 2L, 
    2L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 3L, 3L, 1L, 2L, 2L, 4L, 2L, 
    1L, 4L, 3L, 4L, 2L, 4L, 3L, 4L, 4L, 3L, 2L, 3L, 3L, 2L, 2L, 
    4L, 2L, 4L, 4L, 3L, 4L, 3L, 4L, 1L, 4L, 4L, 4L, 2L, 2L, 3L, 
    3L), .Label = c("0", "20", "40", "60"), class = "factor")), .Names = c("ref", 
"Pupper", "Plower", "PEE", "max_depth", "fangle"), row.names = c(26297L, 
18163L, 13367L, 10757L, 10813L, 43605L, 22984L, 27608L, 39808L, 
11220L, 32882L, 39987L, 35960L, 13719L, 34174L, 25877L, 31747L, 
19402L, 31394L, 14990L, 28537L, 9023L, 13684L, 1781L, 19411L, 
9964L, 41834L, 33800L, 15277L, 8673L, 21864L, 40681L, 35425L, 
11590L, 19901L, 40867L, 36845L, 21698L, 18302L, 470L, 11459L, 
37414L, 24555L, 40026L, 40578L, 33627L, 9525L, 41816L, 17695L, 
42057L, 294L, 9972L, 37137L, 8304L, 19086L, 15817L, 15351L, 26097L, 
24896L, 39059L, 23703L, 20110L, 19937L, 32121L, 28556L, 13L, 
19157L, 42865L, 37922L, 21887L, 31638L, 37008L, 21905L, 41848L, 
26621L, 39864L, 32870L, 39107L, 37721L, 7969L, 23826L, 11903L, 
12024L, 36500L, 38488L, 13287L, 462L, 36245L, 28096L, 38611L, 
21500L, 20565L, 17140L, 27772L, 27773L, 18897L, 30992L, 34564L, 
18553L, 41312L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

您提供的数据子集不足以适应模型,但我认为我可以回答您的问题。横轴上变量的选择由x.var的{​​{1}}参数控制;来自plot.eff()

  

?plot.eff:协变量或因子的索引(数字)或引用名称   放置在效果图的每个面板的水平轴上。该   default是具有最大级别或值的预测变量。

反过来,评估效果的协变量值由x.var的{​​{1}}参数控制,在xlevels中调用Effect();来自effect()

  

?Effect:此参数用于设置任何级别的级别数   焦点预测因子不是一个因素。如果xlevels = NULL,则默认为   那么任何数字预测器的级别数和值都是   由grid.pretty决定。如果xlevels = n是一个整数,那么每个   数字预测器由n个等间隔的级别表示。更多   通常,xlevels可以是一个命名的值列表,可以在其中设置每个值   数字预测器。例如,xlevels = list(x1 = c(2,4,7),x2 = 5)   将使用值2,4和7的等级x1,5等级   x2级别的级别,并使用任何其他数字的默认值   预测。如果计算部分残差,那么焦点   预测器将出现在效果图的水平轴上   在其整个范围内以100个等间距值进行评估,并且,   默认情况下,在分位数处评估其他数字预测变量   在quantiles参数中指定,除非给出它们的值   明确地在xlevels中。

奇怪的是,您显示的第二个图表似乎包含一个未出现在模型中的变量xlevels

我希望这有帮助,

约翰