如何在具有不同颜色的多级分析中显示不同级别 [英] How to display different levels in a multilevel analysis with different colors

查看:84
本文介绍了如何在具有不同颜色的多级分析中显示不同级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是多层次分析的初学者,请尝试了解如何使用base-R中的绘图函数绘制图形.我理解下面的fit的输出,但是我在可视化方面苦苦挣扎. df只是一些简单的测试数据:

I am a beginner at multilevel analysis and try to understand how I can do graphs with the plot functions from base-R. I understand the output of fit below but I am struggeling with the visualization. df is just some simple test data:

t <- seq(0, 10, 1)
df <- data.frame(t = t,
                 y = 1.5+0.5*(-1)^t + (1.5+0.5*(-1)^t) * t,
                 p1 = as.factor(rep(c("p1", "p2"), 10)[1:11]))

fit <- lm(y ~ t * p1, data = df)

# I am looking for an automated version of that:
plot(df$t, df$y)
lines(df$t[df$p1 == "p1"], 
      fit$coefficients[1] + fit$coefficients[2] * df$t[df$p1 == "p1"], col = "blue")
lines(df$t[df$p1 == "p2"], 
      fit$coefficients[1] + fit$coefficients[2] * df$t[df$p1 == "p2"] + 
        + fit$coefficients[3] + fit$coefficients[4] * df$t[df$p1 == "p2"], col = "red")

它应该知道它必须包含p1并且有两行.
结果应如下所示:

It should know that it has to include p1 and that there are two lines.
The result should look like this:

编辑:预测est <- predict(fit, newx = t)的结果与fit相同,但我仍然不知道如何聚类".

Edit: Predict est <- predict(fit, newx = t) gives the same result as fit but still I don't know "how to cluster".

编辑2个@Keith :公式y ~ t * p1读取y = (a + c * p1) + (b + d * p1) * t.对于第一条蓝线",c, d都为零.

Edit 2 @Keith: The formula y ~ t * p1 reads y = (a + c * p1) + (b + d * p1) * t. For the "first blue line" c, d are both zero.

推荐答案

这就是我的方法.我还包括一个ggplot2版本的情节,因为我发现它更适合我对情节的思考方式. 此版本将说明p1中的级别数.如果要补偿模型参数的数量,则只需调整构造xy的方式以包括所有相关变量.我应该指出,如果省略newdata参数,则将对提供给lm的数据集进行拟合.

This is how I would do it. I'm including a ggplot2 version of plot as well because I find it better fitted for the way I think about plots. This version will account for the number of levels in p1. If you want to compensate for the number of model parameters, you will just have to adjust the way you construct xy to include all the relevant variables. I should point out that if you omit the newdata argument, fitting will be done on the dataset provided to lm.

t <- seq(0, 10, 1)
df <- data.frame(t = t,
                 y = 1.5+0.5*(-1)^t + (1.5+0.5*(-1)^t) * t,
                 p1 = as.factor(rep(c("p1", "p2"), 10)[1:11]))

fit <- lm(y ~ t * p1, data = df)

xy <- data.frame(t = t, p1 = rep(levels(df$p1), each = length(t)))
xy$fitted <- predict(fit, newdata = xy)

library(RColorBrewer) # for colors, you can define your own
cols <- brewer.pal(n = length(levels(df$p1)), name = "Set1") # feel free to ignore the warning

plot(x = df$t, y = df$y)
for (i in 1:length(levels(xy$p1))) {
  tmp <- xy[xy$p1 == levels(xy$p1)[i], ]
  lines(x = tmp$t, y = tmp$fitted, col = cols[i])
}

library(ggplot2)
ggplot(xy, aes(x = t, y = fitted, color = p1)) +
  theme_bw() +
  geom_point(data = df, aes(x = t, y = y)) +
  geom_line()

这篇关于如何在具有不同颜色的多级分析中显示不同级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆