在R中绘制vs ggplot2以及如何提取拟合参数 [英] Plot vs ggplot2 in R and how to extract fit parameters

查看:412
本文介绍了在R中绘制vs ggplot2以及如何提取拟合参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为t的data.frame中有以下数据

  DayNum MeanVolume StdDev StdErr 
1 13 207.0500 41.00045 5.125057
2 15 142.7625 27.87236 3.484045
3 18 77.5500 19.43928 2.429910
4 21 66.3750 20.56403 2.570504
5 26 67.0500 29.01576 3.626970
6 29 66.4750 25.94537 3.243171
b 7 33 76.9625 25.31374 3.164218
8 36 91.2875 37.01719 4.627149
9 40 102.0500 29.39898 3.674872
10 43 100.8250 24.22830 3.028538
11 47 120.5125 28.80592 3.600740
12 50 147.8875 35.82894 4.478617
13 54 126.7875 45.43204 5.679004
14 57 139.8500 56.01117 7.001397
15 60 179.1375 69.64526 8.705658
16 64 149.7625 39.10265 4.887831
17 68 229.5250 121.08411 15.135514
18 71 236.5125 76.23146 9.528933
19 75 243.2750 101.69474 12.711842
20 78 331.6750 141.25344 17.656680
21 82 348.2875 122.86359 15.357948
22 85 353.7750 187.24641 23.405801
23 89 385.4000 154.05826 19.257283
24 92 500.9875 263.43714 32.929642
25 95 570.2250 301.82686 37.728358
26 98 692.2250 344.71226 43.089032
27102 692.8000 283.94120 35.492650
28105759.2000 399.19323 49.899153
29109 898.2375 444.94289 55.617861
30112 920.1000 515.79597 64 b

我正在尝试将x = DayNum拟合为y = MeanVolume in t。



这就是我所做的:



适合数据

  model< -lm(log(t $ MeanVolume)〜t $ DayNum,data = t)

图数据

 图(MeanVolume〜DayNum,data = t,ylab =平均体积(mm3),xlim = c(0,120),ylim = c(0,1000))
箭头(t $ DayNum,t $ MeanVolume-t $ StdErr,t $ DayNum,t $ MeanVolume + t $ StdErr,长度= 0.01,角度= 90,c ode = 3)

创建拟合数据

  t $ pred< -exp(预测(模型))

图拟合

  lines(t $ DayNum,t $ pred,col = blue)



另一方面,如果我使用ggplot2通过使用

  ggplot(data = t,mapping = aes(x = DayNum,y = MeanVolume))+ 
geom_line()+
geom_point(size = 3,color = blue)+
geom_smooth(method = glm,method.args = list(family = gaussian) (link = log))))+
labs(x = Days,y =平均体积(mm3),title =数据)+
geom_errorbar(aes(ymin = MeanVolume -StdErr,ymax = MeanVolume + StdErr),width = .2)

我得到以下图/ p>



问题的第二部分:

  library(ggplot2)
p = ggplot(数据= gz,映射= aes(x = DayNum,y = MeanVolume))+
geom_line()+
geom_point(大小= 3 ,color = blue)+
geom_smooth(method = glm,method.args = list(family = gaussian(link = log))))+
labs(x = Days ,y =平均体积(mm3),标题=数据)+
geom_errorbar(aes(ymin = MeanVolume-StdErr,ymax = MeanVolume + StdErr),width = .2)

从ggplot中提取数据可以使用:

  build = ggplot_build(p)

曲线的数据位于 build $ data [[3]]

  p + geom_line(data = build $ data [[3]],aes(x = x,y = y),lty = 2,颜色=红色,大小= 1.5)



此数据与<$ c $中的数据相同c> pred_glm -密度更高(数据点更多)。据我所知,没有什么方法可以从ggplot中仅从预测中提取系数,但是您始终可以如上所述构建glm模型。


I have the following data in a data.frame called t

   DayNum   MeanVolume    StdDev    StdErr
1      13   207.0500  41.00045  5.125057
2      15   142.7625  27.87236  3.484045
3      18    77.5500  19.43928  2.429910
4      21    66.3750  20.56403  2.570504
5      26    67.0500  29.01576  3.626970
6      29    66.4750  25.94537  3.243171
7      33    76.9625  25.31374  3.164218
8      36    91.2875  37.01719  4.627149
9      40   102.0500  29.39898  3.674872
10     43   100.8250  24.22830  3.028538
11     47   120.5125  28.80592  3.600740
12     50   147.8875  35.82894  4.478617
13     54   126.7875  45.43204  5.679004
14     57   139.8500  56.01117  7.001397
15     60   179.1375  69.64526  8.705658
16     64   149.7625  39.10265  4.887831
17     68   229.5250 121.08411 15.135514
18     71   236.5125  76.23146  9.528933
19     75   243.2750 101.69474 12.711842
20     78   331.6750 141.25344 17.656680
21     82   348.2875 122.86359 15.357948
22     85   353.7750 187.24641 23.405801
23     89   385.4000 154.05826 19.257283
24     92   500.9875 263.43714 32.929642
25     95   570.2250 301.82686 37.728358
26     98   692.2250 344.71226 43.089032
27    102   692.8000 283.94120 35.492650
28    105   759.2000 399.19323 49.899153
29    109   898.2375 444.94289 55.617861
30    112   920.1000 515.79597 64.474496

I am trying to fit x = DayNum to y = MeanVolume in t.

Here is what I did:

Fit to data

model<-lm(log(t$MeanVolume) ~ t$DayNum, data=t)

Plot data

plot(MeanVolume~DayNum, data=t, ylab="Mean Volume (mm3)", xlim=c(0,120), ylim=c(0,1000))
arrows(t$DayNum, t$MeanVolume-t$StdErr, t$DayNum, t$MeanVolume+t$StdErr, length=0.01, angle=90, code=3)

Create fit data

t$pred<-exp(predict(model))

Plot fit

lines(t$DayNum,t$pred,col="blue")

On the other hand, if I use ggplot2 to do this by using

ggplot(data = t, mapping = aes(x = DayNum, y=MeanVolume)) + 
  geom_line() + 
  geom_point(size=3, color="blue") + 
  geom_smooth(method="glm", method.args=list(family=gaussian(link="log"))) +
  labs(x="Days", y="Mean Volume (mm3)", title="Data") +
  geom_errorbar(aes(ymin = MeanVolume - StdErr, ymax = MeanVolume + StdErr), width=.2)

I get the following plot

As you can see the fitted curve in the ggplot case is better than in the plot case. Why? Also I would like to fit parameters such as intercept and the slope of the exponential fit line. How can I extract them from ggplot call?

解决方案

lm with log transformed y is not the same as glm with gaussian error distribution and log link (as to why check link in the comment by @Lyngbakr)

gz <- read.table("somet.txt")
gz <- as.data.frame(gz)
model_lm <- lm(log(MeanVolume) ~ DayNum, data = gz)
model_glm <- glm(MeanVolume ~ DayNum, data = gz, family = gaussian(link = "log"))
pred_lm <- exp(predict(model_lm))
pred_glm <- predict(model_glm, type = "response")

plot(MeanVolume ~ DayNum, data = gz, ylab = "Mean Volume (mm3)", xlim = c(0,120), ylim = c(0,1000))
arrows(gz$DayNum, gz$MeanVolume - gz$StdErr, gz$DayNum, gz$MeanVolume + gz$StdErr, length = 0.01, angle = 90, code = 3)

lines(gz$DayNum, pred_lm, col = "blue")
lines(gz$DayNum, pred_glm, col = "red")

legend("topleft", col = c("blue", "red"), lty = 1, legend = c("lm", "glm"))

as for the second part of the question:

library(ggplot2)
p = ggplot(data = gz, mapping = aes(x = DayNum, y=MeanVolume)) + 
  geom_line() + 
  geom_point(size = 3, color="blue") + 
  geom_smooth(method = "glm", method.args = list(family = gaussian(link = "log"))) +
  labs(x = "Days", y = "Mean Volume (mm3)", title = "Data") +
  geom_errorbar(aes(ymin = MeanVolume - StdErr, ymax = MeanVolume + StdErr), width=.2)

to extract the data from a ggplot one can use:

build = ggplot_build(p)

the data for the curve are in build$data[[3]]

p +  geom_line(data = build$data[[3]], aes(x = x, y = y), lty = 2, color = "red", size = 1.5)

This data is the same as data in pred_glm - well its a bit more dense (more data points). As far as I am aware there is no method to extract the coefficients from the ggplot just the predictions, but you can always build the glm model as described above.

这篇关于在R中绘制vs ggplot2以及如何提取拟合参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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