使用ggplot2中的smooth来使用数据中给出的错误来拟合线性模型 [英] Using smooth in ggplot2 to fit a linear model using the errors given in the data

查看:309
本文介绍了使用ggplot2中的smooth来使用数据中给出的错误来拟合线性模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框:

 > dat 
xy yerr
1 -1 -1.132711 0.001744498
2 -2 -2.119657 0.003889120
3 -3 -3.147378 0.007521881
4 -4 -4.220129 0.012921450
5 -5 -4.586586 0.021335644
6 -6 -5.389198 0.032892630
7 -7 -6.002848 0.048230946

我可以用平滑标准误将它绘制为:

  p < -  ggplot(dat,aes (x = x,ymin = y-yerr,ymax = y + yerr),width =(x = x,y = y))+ geom_point()
p < - p + geom_errorbar 0.09)
p + geom_smooth(method =lm,formula = y〜x)


但我需要的是使用 yerr 来适应我的线性模型。是否可以使用ggplot2?

解决方案

好的,我找到了一种方法来回答这个问题。 b $ b

由于在我们收集数据的任何科学实验中,如果该实验正确执行,所有数据值必须有相关的错误。

在某些情况下,所有点的误差方差可能是相等的,但在许多情况下,如原始问题中的当前情况状态,这是不正确的。因此,我们必须在方差中使用不同的方差当为我们的数据拟合曲线时,用于不同测量的误差值。

这样做的方法是将权重归于错误值,根据统计分析方法等于1 / sqrt(errorValue),所以它变成:

  p < -  ggplot(dat,aes(x = x,y = y,weight = 1 / sqrt(yerr))+ 
geom_point()+
geom_errorbar(aes(ymin = y-yerr,ymax = y + yerr),width = 0.09 )+
geom_smooth(method = lm,formula = y〜x)


I have this data frame:

> dat
   x         y        yerr
1 -1 -1.132711 0.001744498
2 -2 -2.119657 0.003889120
3 -3 -3.147378 0.007521881
4 -4 -4.220129 0.012921450
5 -5 -4.586586 0.021335644
6 -6 -5.389198 0.032892630
7 -7 -6.002848 0.048230946

And I can plot it with the standard error smoothing as:

p <- ggplot(dat, aes(x=x, y=y)) + geom_point()
p <- p + geom_errorbar(data=dat, aes(x=x, ymin=y-yerr, ymax=y+yerr), width=0.09)
p + geom_smooth(method = "lm", formula = y ~ x)

But what I need is to use the yerr to fit my linear model. Is it possible with ggplot2?

解决方案

Well, I found a way to answer this.

Since in any scientific experiment where we gather data, if that experiment is correctly executed, all the data values must have an error associated.

In some cases the variance of the error may be equal in all the points, but in many, like the present case states in the original question, that is not true. So we must use that different in the variances of the error values for different measurements when fitting a curve to our data.

That way to do it is to attribute the weight to the error values, which according to statistical analysis methods are equal to 1/sqrt(errorValue), so, it becomes:

p <- ggplot(dat, aes(x=x, y=y, weight = 1/sqrt(yerr))) + 
    geom_point() + 
    geom_errorbar(aes(ymin=y-yerr, ymax=y+yerr), width=0.09) + 
    geom_smooth(method = "lm", formula = y ~ x)

这篇关于使用ggplot2中的smooth来使用数据中给出的错误来拟合线性模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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