简体   繁体   English

使用高斯拟合 R 拟合和误差条

[英]Fitting and error bars with a gaussian fitting R

I am trying to fit some data in R and so far I think I am doing an ok-job.我正在尝试将一些数据放入 R 中,到目前为止,我认为我做得还不错。 My dataset is as follow (source:我的数据集如下(来源:

x<-c(1:35)
y<-c(221,88,76,203,233,228,288,498,428,443,570,640,1145,1326,1598,
529,2076,2249,2116,2795,2853,2470,2989,2648,4480,4670,4821,
3957,3780,3612,3491,4492,4401,3651,3815)

data<-data.frame(x,y)
fitG =
   function(x,y,mu,sig,scale){
                f = function(p){
                  d = p[3]*dnorm(x,mean=p[1],sd=p[2])
                  sum((d-y)^2)
                  }
                 optim(c(mu,sig,scale),f)
   }

fitP=fitG(data$x, data$y, 35, 1, 6000)

plot(data$x, data$y)
p =fitP$par
lines(data$x, p[3]*dnorm(data$x, p[1], p[2]))

(source, http://www.protezionecivile.gov.it , i'm using the covid data as an example) (来源, http://www.protezionecivile.gov.it ,我以 covid 数据为例)

which fit the data quite nicely... but is there a way to它非常适合数据......但是有没有办法

a) instead of using the plot function using the ggplot2 package which allows for more customization of the plot and b) I want to do a fitting for the data not shown here (ie extend the gaussian curve beyond the day 35), possibly adding an error bar that show the uncertainty over the fitting over time... I have tried to look up online but all the articles I've found assume that the reader have a strong statistical background... which I do not. a) 而不是使用使用 ggplot2 包的 plot 函数,它允许对绘图进行更多的自定义和 b) 我想对此处未显示的数据进行拟合(即,将高斯曲线扩展到第 35 天之后),可能会添加一个误差条显示拟合的不确定性随着时间的推移......我试图在网上查找,但我发现的所有文章都假设读者具有很强的统计背景......我没有。

To use ggplot, you can do:要使用 ggplot,您可以执行以下操作:

  data %>% 
  ggplot(aes(x=x, y=y))+
  geom_point()+
  geom_smooth()

And then customize as you wish.然后根据需要进行自定义。

在此处输入图片说明

You can add the fit onto the data.frame and plot:您可以将拟合添加到 data.frame 并绘制:

library(ggplot2)
data$fitted = p[3]*dnorm(data$x, p[1], p[2])
ggplot(data,aes(x=x,y=y)) + 
geom_point() + 
geom_line(aes(y=fitted),col="blue")

在此处输入图片说明

Now as for the uncertainty, what you are fitting is not a conventional distribution (because you have a third parameter scale), and I don't see how a normal distribution can help you make predictions here.现在至于不确定性,您拟合的不是常规分布(因为您有第三个参数标度),我看不出正态分布如何帮助您在此处进行预测。

What exactly do you mean by "possibly adding an error bar that show the uncertainty over the fitting over time" ? “可能添加一个误差条,显示拟合随时间的不确定性”究竟是什么意思? Although it's a lot of statistics, it can be simplified into this, what is the error do you want to show?虽然统计了很多,但是可以简化成这样,你想显示的错误是什么?

You can use the solution shown by @johnjohn, just bear in mind it is a gam which is fitting a spline and it's errors are estimated from the errors of the fit, you can see more about it maybe on this blog .您可以使用@johnjohn 显示的解决方案,请记住这是一个拟合样条曲线的gam ,它的误差是根据拟合误差估计的,您可以在此博客上了解更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM