最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to extract data from a regression model plot in R? - Stack Overflow

programmeradmin1浏览0评论

this is my code:

library(tidyverse)
if (!require(performance) ) { 
       install.packages("performance");library(performance)} 
if (!require(see) ) {install.packages("see");library(see)} 
# defining a model
model <- lm(mpg ~ wt + am + gear + vs * cyl, data = mtcars)

check_model(model)

How can I obtain the dataframe related to the Normality of Residuals Chart?

I need to replicate this chart and I dont know how to extract the data.

Any help?

this is my code:

library(tidyverse)
if (!require(performance) ) { 
       install.packages("performance");library(performance)} 
if (!require(see) ) {install.packages("see");library(see)} 
# defining a model
model <- lm(mpg ~ wt + am + gear + vs * cyl, data = mtcars)

check_model(model)

How can I obtain the dataframe related to the Normality of Residuals Chart?

I need to replicate this chart and I dont know how to extract the data.

Any help?

Share Improve this question edited Mar 15 at 23:13 IRTFM 264k22 gold badges379 silver badges500 bronze badges asked Mar 14 at 22:48 LauraLaura 63117 silver badges43 bronze badges 5
  • 2 The docx for check_model() say that it returns the frame used for plotting. Have you tried res <- check_model(model) and look at res? – r2evans Commented Mar 14 at 22:53
  • @r2evans yes. This is the only one that I couldnt find the dataset – Laura Commented Mar 14 at 22:54
  • 1 Never heard this term 'Normality of Residuals Chart' – Friede Commented Mar 14 at 22:57
  • @Friede I think somehow I will need to use the others data, manipulate them and find the data for this plot – Laura Commented Mar 14 at 23:02
  • @Friede They are just giving a fancy name to a QQ-plot of the residuals against an assumption that they are distributed as N(0,1), i.e. as Normal with mean of 0 and variance of 1. – IRTFM Commented Mar 15 at 23:15
Add a comment  | 

1 Answer 1

Reset to default 7

You could dig around in the code of see:::plot.see_check_normality, but it's pretty complicated. The easier/hackier alternative is to use ggplot_build to export the data used in the ggplot object, as below ...

library(performance)
library(ggplot2)
m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
result <- check_normality(m)
p <- plot(result)
print(p)

g <- ggplot_build(p)
d <- g$data[[2]]
plot(d$x, d$y)

@stefan points out that d <- layer_data(p, i = 2) will retrieve the same information (if you omit the p argument it will take the data from the last ggplot rendered, but I think it's better to be explicit)

发布评论

评论列表(0)

  1. 暂无评论