R-具有3个显性变量的线性混合效应模型的回归平面

时间:2019-09-18 09:58:41

标签: r regression plane

我正在努力绘制带有3个显性变量的线性混合效应模型的回归平面。我使用了代码here,该代码用于z〜x + y模型,并尝试将其改编为z〜x + y + c模型:

library(nlme)
library(plot3D)

data <- read.table('data-plot-3D.csv', dec=',', sep=';', header=TRUE)    

test <- lme(z~x+y+c,
            data=data,
            random=~1|s, method="ML")

grid.lines = nrow(data)
x.pred <- seq(min(data$x), max(data$x), length.out = grid.lines)
y.pred <- seq(min(data$y), max(data$y), length.out = grid.lines)
c.pred <- seq(min(data$c), max(data$c), length.out = grid.lines)

xyc <- expand.grid(x=x.pred, 
                   y=y.pred, 
                   c=c.pred)

z.pred <- matrix(predict(test, newdata=xyc, level=0), 
                  nrow=grid.lines, ncol=grid.lines)

fitpoints <- as.vector(predict(test, data))

scatter3D(data$x, data$y, data$z, 
          pch=18, cex=2, theta=20, phi=20, ticktype="detailed",
          xlab="x", ylab = "y", zlab="z",  
          surf=list(x=x.pred,
                    y=y.pred,
                    z=z.pred,
                    facets = NA,
                    fit = fitpoints),
          main="Model 3D plot")

但是这个数字是错误的:这架飞机根本不合适。我有几个假设可以解释这个结果,例如,在预测值时使用level=0参数,或者我不知道的线性混合效应模型的某些属性。如果我理解正确,则需要level=0参数,因为lme模型包含一些噪声并且我的绘图数据没有考虑到它(也许我错了)。

当我将模型简化为简单的线性模型(lm而不是lme),并去除了3d变量(z~x+y)时,我得到了正确的回归平面:

library(nlme)
library(plot3D)

data <- read.table('data-plot-3D.csv', dec=',', sep=';', header=TRUE)

test <- lm(z~x+y, data=data)

grid.lines = nrow(data)
x.pred <- seq(min(data$x), max(data$x), length.out = grid.lines)
y.pred <- seq(min(data$y), max(data$y), length.out = grid.lines)

xy <- expand.grid(x=x.pred, 
                  y=y.pred)

z.pred <- matrix(predict(test, newdata=xy), 
                 nrow=grid.lines, ncol=grid.lines)

fitpoints <- predict(test)

scatter3D(data$x, data$y, data$z, 
          pch=18, cex=2, theta=20, phi=20, ticktype="detailed",
          xlab="senescence", ylab = "cambium", zlab="budburst",  
          surf=list(x=x.pred,
                    y=y.pred,
                    z=z.pred,
                    facets = NA,
                    fit = fitpoints),
          main="Good plane (lm z~x+y)")

我该如何解决这个问题?数据和图片如下。我还计划根据c变量而不是沿z轴为点和平面着色,但是我想它不太重要(也许我会问另一个问题)

s;z;x;y;c
B;103,5104;259,9426;250;0,289661996
F;100,6323;292,9711;250;0,522028213
F;105,2674;293,2956;257;0,429718346
F;103,2945;289,3861;257;0,499746521
B;100,3786;283,8451;271;0,210084525
B;100,392;284,7398;271;0,261014107
F;106,0284;295,9574;271;0,674816959
F;105,2831;280,6875;271;0,592056388
F;109,1873;284,9641;271;0,607971883
B;100,1647;253,8401;271;0,327859183
B;100,1124;259,4129;271;0,270563403
B;101,2346;261,3593;271;0,238732415
B;100,9574;285,814;277;0,261014107
F;105,1347;294,102;277;0,461549335
B;102,3565;302,9692;277;0,33422538
F;102,6884;297,6776;284;0,410619753
F;109,0465;288,4876;284;0,44881694
Q;110,0359;318,2187;284;0,630253575
Q;108,2855;285,7081;311;0,413802852
F;113,5328;308,7703;311;0,385154962
F;105,1965;266,3839;311;0,585690191
Q;112,9197;314,6168;311;0,604788784
Q;105,5601;284,8472;311;0,506112719
Q;109,5241;309,2628;319;0,36287327
Q;109,5802;315,2328;319;0,461549335
F;108,3161;285,0408;319;0,397887358
Q;110,3092;317,1796;328;0,423352149
Q;110,5984;297,9926;328;0,620704278

wrong plane

good plane

0 个答案:

没有答案