对于固定值 `Petal.Length` 和 `Species`,我如何绘制 `Sepal.Length` 和 `Sepal.Width` 之间的关系?
How can I plot the relationship between `Sepal.Length` and `Sepal.Width` for a fixed value of `Petal.Length` and by `Species`?
我是 运行 LME,我想绘制我的响应变量与我的一个预测变量之间的关系,保持另一个预测变量不变,并通过 ID
(被视为随机因素)。
类似地,使用iris
dataframe 示例,它对应于:
library(nlme)
library(ggplot2)
df <- iris[,c("Sepal.Length","Sepal.Width","Petal.Length","Species")]
mod <-nlme::lme(Sepal.Length ~ Sepal.Width * Petal.Length, random= ~ 1|Species, data = df,method="REML")
如您所见,species
用作随机因子。在这个例子中,我想绘制的是 Sepal.Length
和 Sepal.Width
之间的线性关系保持不变 Petal.Length
和 species
.
我试过这个:
Plot <- ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod, Petal.Length=mean(iris$Petal.Length)), group=Species), size=2.1)
Plot
然而,情节和我这样做是一样的:
Plot <- ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod), group=Species), size=2.1)
Plot
所以,我没有保持不变Petal.Length
。我也知道我没有保持不变 Petal.Length
因为我没有得到每个物种的直线。也就是说,Petal.Length
的合并使线条变得不规则,因为当我在我的模型中不考虑 Petal.Length
时,我得到这个:
mod.b <-nlme::lme(Sepal.Length ~ Sepal.Width, random= ~ 1|Species, data = df,method="REML")
Plot <- ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod.b), group=Species), size=2.1)
Plot
那么,有人知道如何在根据随机因素绘制 LME
模型的线性预测时保持一个预测变量 (Petal.Length
) 不变吗?
如有任何帮助,我将不胜感激
我想这就是您要找的:
library(ggplot2)
newdf <- df
newdf$Petal.Length <- ave(newdf$Petal.Length, newdf$Species)
ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod, newdf), group=Species), size=2.1)
备注:
- 您以错误的方式使用了
predict
函数。您需要添加一组全新的数据,而不仅仅是一列。此外,您编写新专栏的方式是错误的,因为您实际上为 predict
函数提供了一个名为 Petal.Length
的参数,该参数属于 ...
,因此被忽略。
- 我用
ave
计算了每组的平均值,否则你的结果就没有意义。如果您尝试通过以这种方式计算 Petal.Length
的平均值来绘制图表 newiris$Petal.Length <- mean(newiris$Petal.Length)
,您就会明白我的意思。
当你写:
ave(newdf$Petal.Length, newdf$Species)
ave
为 newdf$Species
定义的每个组计算 newdf$Petal.Length
的平均值
我是 运行 LME,我想绘制我的响应变量与我的一个预测变量之间的关系,保持另一个预测变量不变,并通过 ID
(被视为随机因素)。
类似地,使用iris
dataframe 示例,它对应于:
library(nlme)
library(ggplot2)
df <- iris[,c("Sepal.Length","Sepal.Width","Petal.Length","Species")]
mod <-nlme::lme(Sepal.Length ~ Sepal.Width * Petal.Length, random= ~ 1|Species, data = df,method="REML")
如您所见,species
用作随机因子。在这个例子中,我想绘制的是 Sepal.Length
和 Sepal.Width
之间的线性关系保持不变 Petal.Length
和 species
.
我试过这个:
Plot <- ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod, Petal.Length=mean(iris$Petal.Length)), group=Species), size=2.1)
Plot
然而,情节和我这样做是一样的:
Plot <- ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod), group=Species), size=2.1)
Plot
所以,我没有保持不变Petal.Length
。我也知道我没有保持不变 Petal.Length
因为我没有得到每个物种的直线。也就是说,Petal.Length
的合并使线条变得不规则,因为当我在我的模型中不考虑 Petal.Length
时,我得到这个:
mod.b <-nlme::lme(Sepal.Length ~ Sepal.Width, random= ~ 1|Species, data = df,method="REML")
Plot <- ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod.b), group=Species), size=2.1)
Plot
那么,有人知道如何在根据随机因素绘制 LME
模型的线性预测时保持一个预测变量 (Petal.Length
) 不变吗?
如有任何帮助,我将不胜感激
我想这就是您要找的:
library(ggplot2)
newdf <- df
newdf$Petal.Length <- ave(newdf$Petal.Length, newdf$Species)
ggplot(df, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point(size=1.7,alpha=0.6) +
geom_line(aes(y=predict(mod, newdf), group=Species), size=2.1)
备注:
- 您以错误的方式使用了
predict
函数。您需要添加一组全新的数据,而不仅仅是一列。此外,您编写新专栏的方式是错误的,因为您实际上为predict
函数提供了一个名为Petal.Length
的参数,该参数属于...
,因此被忽略。 - 我用
ave
计算了每组的平均值,否则你的结果就没有意义。如果您尝试通过以这种方式计算Petal.Length
的平均值来绘制图表newiris$Petal.Length <- mean(newiris$Petal.Length)
,您就会明白我的意思。
当你写:
ave(newdf$Petal.Length, newdf$Species)
ave
为 newdf$Species
newdf$Petal.Length
的平均值