在处理R中ANOVA类型的研究设计时,如何绘制具有观察值的拟合模型? [英] How can I plot fitted models with observations when dealing with the ANOVA type of research design in R?

查看:274
本文介绍了在处理R中ANOVA类型的研究设计时,如何绘制具有观察值的拟合模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何将多层次建模应用于传统的ANOVA研究设计。
我想用ggplot2绘制每种治疗的拟合回归线。我想绘制基于我拟合的模型的回归线,不要让ggplot2绘图,因为我想看看如何根据不断变化的模型估计不同。我知道我可以自己计算系数和斜率,但由于模型相对复杂,我正在寻找更容易绘图的方法。

这是样本我正在处理的研究设计类型的代码。
我发现sjPlot包(



或者,像这样:

  sjp .lm(fit,type =pred,
vars = c(test_timing,treatmen t),facet.grid = T)



此外,帮助文件(参见



点图



 #plot dots 
ggplot(me,aes(x = x,y = predicted,color = group))+
geom_point()



带误差线的点图



 #add CI,需要抖动
ggplot(me,aes(x = x,y = expected,color = group ))+
geom_point(position = position_dodge(.2))+
geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(.2) )



构面



 #构面
ggplot(me,aes(x = x,y =预测,颜色=组))+
geom_point(position = position_dodge(.2))+
geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(.2))+
facet_grid(〜group)



plot() - ggeffects包中的函数

 #简单的方法
plot(me)



原始观察



ggpredict()将原始数据作为属性返回给返回值,因此您还可以将原始观察结果绘制为那么:

  raw < -  attr(me,rawdata)

ggplot(me, aes(x = x,y = expected,color = group))+
geom_point(position = position_dodge(.2))+
geom_errorbar(aes(ymin = conf.low,ymax = conf.high ),width = 0,position = position_dodge(.2))+
#使用jitter作为observa tions here
geom_jitter(data = raw,mapping = aes(x = x,y = response,color = group))



或者,简单的方法:

  plot(me,rawdata = TRUE)

例如,不同的绘图选项使用 jitter = FALSE 来消除原始数据点的抖动等。

I am learning how to apply a multilevel modelling to traditional ANOVA research designs. I would like to plot fitted regression lines for each treatment using ggplot2. I would like to plot regression lines based on models I fitted, not let ggplot2 to plot since I would like to see how estimations differ based on changing models. I am aware that I can calculate coefficients and slopes by myself, but because the model is relatively complicated, I am looking for ways to plot more easily.

This is the sample code for the type of research design I am dealing with. I found that the sjPlot package (http://www.strengejacke.de/sjPlot/sjp.lm/) provides very nice plots, which shows the regression lines for each treatment over each test timing and actual observations on a scatter plot. This is exactly what I would like to make with ggplot.

 require(tidyverse)
 require(sjPlot)
 set.seed (100)
 dat <- data_frame(
    participant_id = c(c(1:15), c(1:15)),
    treatment = c(sample (letters [1:3], 15, replace = T), sample (letters [1:3], 15, replace = T)),
    test_timing = c(sample(letters [1:3], 15, replace = T),sample(letters [1:3], 15, replace = T)),
    learning_gain = (runif(30, min = 1, max = 20))
)

fit <- lm (learning_gain ~ treatment * test_timing -1, data = dat)
sjp.lm(fit, type = "pred", 
vars = c("test_timing", "treatment"),facet.grid = F)

Or, something like this:

 sjp.lm(fit, type = "pred", 
    vars = c("test_timing", "treatment"),facet.grid = T)

I would really appreciate it if you could teach me how I can make a plot similar to this image using the ggplot2 package. Thank you!

解决方案

Indeed, sjPlot just calls a function from ggeffects. ggeffects again just returns a data frame with the marginal effects / predicted values. So you can use this data frame to build your own ggplot-objects, or simply use the plot()-function to create a plot (a ggplot-object).

There is a package vignette that describes how to build own plots using ggplot: https://cran.r-project.org/web/packages/ggeffects/vignettes/marginaleffects.html

Furthermore, the help-files (see https://cran.r-project.org/web/packages/ggeffects/ggeffects.pdf) are full of examples on how to to create plots using ggplot. Maybe this helps you?

Here is an example for your specific case, but note that you will not see straight lines, because your variables are categorical:

require(tidyverse)
require(ggeffects)
set.seed (100)
dat <- data_frame(
  participant_id = c(c(1:15), c(1:15)),
  treatment = c(sample (letters [1:3], 15, replace = T), sample (letters [1:3], 15, replace = T)),
  test_timing = c(sample(letters [1:3], 15, replace = T),sample(letters [1:3], 15, replace = T)),
  learning_gain = (runif(30, min = 1, max = 20))
)

# fit model
fit <- lm (learning_gain ~ treatment * test_timing -1, data = dat)

# compute marginal effects
me <- ggpredict(fit, c("test_timing", "treatment"))

# see results
me

# A tibble: 9 x 5
      x predicted conf.low conf.high group
  <dbl>     <dbl>    <dbl>     <dbl> <fct>
1  1.00      9.81   -0.472      20.1 a    
2  1.00      9.29    4.15       14.4 b    
3  1.00     13.2     5.95       20.5 c    
4  2.00     14.1     8.93       19.2 a    
5  2.00     11.6     7.00       16.2 b    
6  2.00      7.23   -3.06       17.5 c    
7  3.00     13.7     7.77       19.6 a    
8  3.00     13.5     8.33       18.6 b    
9  3.00     10.9     6.66       15.1 c    

Line plot

# note that predictors are categorical, so no straight line
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_line()

Dot plot

# plot dots
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point()

Dot plots with error bars

# add CI, need jitter
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2))

Facets

# facets
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2)) +
  facet_grid(~group)

plot()-function from the ggeffects package

# the simple way
plot(me)

Raw Observations

ggpredict() returns the raw data as attribute to the return value, so you can also plot the original observations as well:

raw <- attr(me, "rawdata")

ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2)) +
  # using jitter for observations here
  geom_jitter(data = raw, mapping = aes(x = x, y = response, colour = group))

Or, also the simple way:

plot(me, rawdata = TRUE)

See also ?plot.ggeffects for the different plot options, for instance, use jitter = FALSE to remove the jittering for the raw data points etc...

这篇关于在处理R中ANOVA类型的研究设计时,如何绘制具有观察值的拟合模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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