我用ezANOVA运行了一种三向重复测量ANOVA。 我正在尝试通过qqplot观察残差发生了什么,但是我不知道如何到达残差或者它们是否在那里。 使用我的LME模型,我只是从模型中提取它们 并绘制它们 我想使用ggplot,因为我想保持图形的一致性。 编辑 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在尝试使用 ggplot2 function 绘制混合效应 model 的残余效应。 但是,在执行搜索后,我发现了一些可用的功能,但在我看来,对于 function nlme 它们不起作用。
我打算制作的图表是以下示例的图表:
数据在这里。
数据: https://drive.google.com/file/d/19mykz4B7jkTilbtwPQb3NUI09YZwohhs/view?usp=sharing
我最初尝试的计算例程如下,请参阅执行 ggplot2 中的 function 时出现的错误。
library(splines)
library(ggplot2)
library(nlme)
library(gridExtra)
setwd("C:\\Users\\Desktop")
datanew1 = read.table("dadosnew.csv", header = T, sep=";", dec = ",")
datanew1$DummyVariable = as.factor(datanew1$DummyVariable)
datanew1$Variable2 = as.factor(datanew1$Variable2)
datanew1$Variable3 = as.factor(datanew1$Variable3)
#############################################################################
############################## Model ########################################
#############################################################################
model <- lme(Response~(bs(Variable1, df=3)) + DummyVariable,
random=~1|Variable2/Variable3, datanew1, method="REML")
completemodel <- update(model, weights = varIdent(form=~1|DummyVariable))
p1 <- qplot(.fitted, .resid, data = completemodel) +
geom_hline(yintercept = 0) +
geom_smooth(se = FALSE)
Erro: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class lme
Run `rlang::last_error()` to see where the error occurred.
p2 <- qplot(sample =.stdresid, data = completemodel, stat = "qq") + geom_abline()
grid.arrange(p1,p2)
Erro: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class lme
Run `rlang::last_error()` to see where the error occurred.
Além disso: Warning message:
`stat` is deprecated
我尝试制作图表的另一种方法是使用下面的 function,但我没有成功。
ggplot(completemodel, aes(.fitted, .resid)) + geom_point()
Erro: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class lme
Run `rlang::last_error()` to see where the error occurred.
[解决方案]
library(splines)
library(ggplot2)
library(nlme)
library(gridExtra)
datanew1 = read.table("E:/Downloads/dadosnew.csv", header = T, sep=";", dec = ",")
datanew1$DummyVariable = as.factor(datanew1$DummyVariable)
datanew1$Variable2 = as.factor(datanew1$Variable2)
datanew1$Variable3 = as.factor(datanew1$Variable3)
model <- lme(Response~(bs(Variable1, df=3)) + DummyVariable,
random=~1|Variable2/Variable3, datanew1, method="REML")
completemodel <- update(model, weights = varIdent(form=~1|DummyVariable))
df_model <- broom.mixed::augment(completemodel)
#> Registered S3 method overwritten by 'broom.mixed':
#> method from
#> tidy.gamlss broom
df_model[".stdresid"] <- resid(completemodel, type = "pearson")
p1 <- ggplot(df_model, aes(.fitted, .resid)) +
geom_point() +
geom_hline(yintercept = 0) +
geom_smooth(se=FALSE)
p2 <- ggplot(df_model, aes(sample = .stdresid)) +
geom_qq() +
geom_qq_line()
grid.arrange(p1,p2)
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.