Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot绘制截距和坡度的TIBLE的多元线性回归_R_Ggplot2 - Fatal编程技术网

R 使用ggplot绘制截距和坡度的TIBLE的多元线性回归

R 使用ggplot绘制截距和坡度的TIBLE的多元线性回归,r,ggplot2,R,Ggplot2,我对多重线性回归的截距alpha和斜率beta进行了tibblefit。Dput数据如下 我想在同一个绘图窗口中对每个回归进行指数化和绘图,以便y=exp(alpha+beta*x) 下面是我制作的一个“测试”图,以找出如何做到这一点: ggplot() + stat_function(fun = ~ for (i in 1:nrow(fit)) { exp(fit$alpha[i] + fit$beta[i] * seq(0, 10, .01)) }) + theme_cl

我对多重线性回归的截距
alpha
和斜率
beta
进行了tibble
fit
。Dput数据如下

我想在同一个绘图窗口中对每个回归进行指数化和绘图,以便
y=exp(alpha+beta*x)

下面是我制作的一个“测试”图,以找出如何做到这一点:

ggplot() +
  stat_function(fun = ~ for (i in 1:nrow(fit)) {
    exp(fit$alpha[i] + fit$beta[i] * seq(0, 10, .01))
  }) +
  theme_classic() +
  xlim(0, 10) +
  ylim(0, 1000)
但它不起作用,我得到以下警告,我不确定是否理解:

Computation failed in `stat_function()`:
Elements must equal the number of rows or 1 
另外,是否有一个
ggplot
函数可以直接迭代每个回归,这样我就不必使用循环?我知道,
geom_abline()
可以用截距和坡度来做,但我不能对回归进行指数化

数据:


利用
purrr::map
geom_函数
可以这样实现:

库(ggplot2)
ggplot()+
purrr::map(1:nrow(fit),~geom_函数(fun=函数(x)exp(fit$alpha[.x]+fit$beta[.x]*x)))+
主题(经典)+
xlim(0,10)+
ylim(0,1000)

利用
purrr::map
geom_函数
可以这样实现:

库(ggplot2)
ggplot()+
purrr::map(1:nrow(fit),~geom_函数(fun=函数(x)exp(fit$alpha[.x]+fit$beta[.x]*x)))+
主题(经典)+
xlim(0,10)+
ylim(0,1000)

在绘制ggplot之前,您可以通过以下方式计算x和y:

library(dplyr)
library(ggplot2)

fit %>%
 mutate(model = row_number()) %>%
 rowwise(model, alpha, beta) %>%
 summarise(x = seq(0, 10, .01),
           y = exp(alpha + beta * x)) %>% 
 
 ggplot() +
 geom_line(aes(x = x, y = y, colour = factor(model)), show.legend = FALSE) +
 theme_classic() +
 xlim(0, 10) +
 ylim(0, 1000)


编辑:

只需快速更新,以查看使用
dplyr
purr
的方法在效率方面的差异:

microbenchmark::microbenchmark(
dplyr = fit %>%
 mutate(model = row_number()) %>%
 rowwise(model, alpha, beta) %>%
 summarise(x = seq(0, 10, .01),
           y = exp(alpha + beta * x), 
           .groups = "drop") %>% 
 
 ggplot() +
 geom_line(aes(x = x, y = y, colour = factor(model)), show.legend = FALSE) +
 theme_classic() +
 xlim(0, 10) +
 ylim(0, 1000),


purr = ggplot() +
 purrr::map(1:nrow(fit), ~ geom_function(fun = function(x) exp(fit$alpha[.x] + fit$beta[.x] * x))) +
 theme_classic() +
 xlim(0, 10) +
 ylim(0, 1000)

) %>% plot()


dplyr
解决方案的速度要快得多。

您可以通过以下方式在ggplot之前计算x和y:

library(dplyr)
library(ggplot2)

fit %>%
 mutate(model = row_number()) %>%
 rowwise(model, alpha, beta) %>%
 summarise(x = seq(0, 10, .01),
           y = exp(alpha + beta * x)) %>% 
 
 ggplot() +
 geom_line(aes(x = x, y = y, colour = factor(model)), show.legend = FALSE) +
 theme_classic() +
 xlim(0, 10) +
 ylim(0, 1000)


编辑:

只需快速更新,以查看使用
dplyr
purr
的方法在效率方面的差异:

microbenchmark::microbenchmark(
dplyr = fit %>%
 mutate(model = row_number()) %>%
 rowwise(model, alpha, beta) %>%
 summarise(x = seq(0, 10, .01),
           y = exp(alpha + beta * x), 
           .groups = "drop") %>% 
 
 ggplot() +
 geom_line(aes(x = x, y = y, colour = factor(model)), show.legend = FALSE) +
 theme_classic() +
 xlim(0, 10) +
 ylim(0, 1000),


purr = ggplot() +
 purrr::map(1:nrow(fit), ~ geom_function(fun = function(x) exp(fit$alpha[.x] + fit$beta[.x] * x))) +
 theme_classic() +
 xlim(0, 10) +
 ylim(0, 1000)

) %>% plot()


dplyr
解决方案速度更快。

太好了,谢谢!我不知道我可以使用
map()
ggplot
这样的函数。很高兴知道。太好了,谢谢!我不知道我可以使用
map()
ggplot
这样的函数。很高兴知道。谢谢你的意见。这是可行的,但我觉得@stefan方法更简单。好吧,很公平。如果你对时间效率感兴趣,我会更新我的答案,让你看到不同之处。谢谢你的意见。这是可行的,但我觉得@stefan方法更简单。好吧,很公平。如果你对时间效率感兴趣,我会更新我的答案,让你看到不同之处。