试图用ggplot2的geom_smooth()显示原始和拟合的数据(nls + dnorm) [英] trying to display original and fitted data (nls + dnorm) with ggplot2's geom_smooth()

查看:605
本文介绍了试图用ggplot2的geom_smooth()显示原始和拟合的数据(nls + dnorm)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索一些数据,所以我想要做的第一件事就是试着去适应正常的(高斯)分布。这是我第一次在R中尝试这个,所以我一次只采取一步。首先我预存了我的数据:

  myhist = data.frame(size = 10:27,counts = c(1L, 3L,5L,6L,9L,14L,13L,23L,31L,40L,42L,22L,14L,7L,4L,2L,2L,1L))

qplot(x = size,y = counts,data = myhist)



因为我想要计数,我需要添加归一化因子(N)来放大密度: / p>

  fit = nls(counts〜N * dnorm(size,m,s),data = myhist,start = c(m = 20,s = 5,N = sum(myhist $ counts)))

然后我创建了拟合数据显示和一切工作很好:

  x = seq(10,30,0.2)
拟合=数据。 frame(size = x,counts = predict(fit,data.frame(size = x)))
ggplot(data = myhist,aes(x = size,y = counts))+ geom_point()+ geom_line data = fitted)



我很激动wh我发现这个讨论使用geom_smooth()来完成所有步骤的线程,但我无法实现它:





下面是我尝试的...和我得到:

pre $ g $ p $ ggplot(data = myhist,aes(x = size,y = counts))+ geom_point()+ geom_smooth(method =nls,formula = counts〜N * dnorm(size,m,s),se = F,start = list(m = 20,s = 5,N = 300,size = 10))

方法(公式,data = data,weights = weight,...)中的错误:
参数在'data'中没有起始值:counts

这个错误似乎表明它试图适合观察变量计数,但这没有任何意义,如果我也为计数指定一个起始值,它可以预见地会吓坏了:

 拟合参数'm','s','N','size','counts'没有任何变量

eval中的错误(expr,envir,enclos):找不到对象'counts'

任何想法我做错了?当然,这不是世界末日,但更少的步骤总是更好,而且你们总是为这些常见任务提出最优雅的解决方案。



谢谢提前!!

Jeffrey

解决方案

第一个错误表明ggplot2在数据中找不到变量'count',它用于公式中。



统计数据在映射后发生,即大小 - > x,并计数 - > y。



以下是在geom_smooth中使用nls的示例:

  ggplot(data = myhist,aes(x = size,y = counts))+ geom_point()+ 
geom_smooth(method =nls,formula = y〜N * dnorm(x,m,s), se = F,
start = list(m = 20,s = 5,N = 300))

关键是在公式的规范中使用x和y,而不是大小和数量。


I am exploring some data, so the first thing I wanted to do was try to fit a normal (Gaussian) distribution to it. This is my first time trying this in R, so I'm taking it one step at a time. First I pre-binned my data:

myhist = data.frame(size = 10:27, counts = c(1L, 3L, 5L, 6L, 9L, 14L, 13L, 23L, 31L, 40L, 42L, 22L, 14L, 7L, 4L, 2L, 2L, 1L) )

qplot(x=size, y=counts, data=myhist)

Since I want counts, I need to add a normalization factor (N) to scale up the density:

fit = nls(counts ~ N * dnorm(size, m, s), data=myhist, start=c(m=20, s=5, N=sum(myhist$counts)) )   

Then I create the fitted data for display and everything works great:

x = seq(10,30,0.2)
fitted = data.frame(size = x, counts=predict(fit, data.frame(size=x)) )
ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() + geom_line(data=fitted)

I got excited when I found this thread which talks about using geom_smooth() to do it all in one step, but I can't get it to work:

Here's what I try... and what I get:

ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() + geom_smooth(method="nls", formula = counts ~ N * dnorm(size, m, s), se=F, start=list(m=20, s=5, N=300, size=10))

Error in method(formula, data = data, weights = weight, ...) : 
  parameters without starting value in 'data': counts

The error seems to indicate that it's trying to fit for the observed variable, counts, but that doesn't make any sense, and it predictably freaks out if I specify a "starting" value for counts too:

fitting parameters ‘m’, ‘s’, ‘N’, ‘size’, ‘counts’ without any variables

Error in eval(expr, envir, enclos) : object 'counts' not found

Any idea what I'm doing wrong? It's not the end of the world, of course, but fewer steps are always better, and you guys always come up with the most elegant solutions to these common tasks.

Thanks in advance!

Jeffrey

解决方案

the first error indicates that ggplot2 cannot find the variable 'count', which is used in formula, in data.

Stats take place after mapping, that is, size -> x, and counts -> y.

Here is an example for using nls in geom_smooth:

ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() + 
  geom_smooth(method="nls", formula = y ~ N * dnorm(x, m, s), se=F, 
              start=list(m=20, s=5, N=300)) 

The point is that using x and y, instead of size and counts, in the specification of formula.

这篇关于试图用ggplot2的geom_smooth()显示原始和拟合的数据(nls + dnorm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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