在ggplot2中使用nls的pred $ fit错误 [英] Error with pred$fit using nls in ggplot2

查看:134
本文介绍了在ggplot2中使用nls的pred $ fit错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,使用ggplot2中的nls来绘制功率曲线代码如下:

  mass <-c(4120, 4740,5550,5610,6520,6870,7080,8500,8960,10350,10480,10550,11450,11930,12180,13690,13760,13800,14050,14700,15340,15790,15990,17300,18460,18630, 18650,20050,23270,24530,25030,27540,28370,33460,33930,34450,34500)

solv_acc <-c(2760,2990,2990,3180,3900,4010,4140, 4680,4750,5330,4980,5860,5930,5570,5910,6790,6690,7020,6240,6620,6600,6860,7940,7600,8250,8530,7410,9160,9140,10300,10440,10390, 11020,12640,11920,12110,12650)

df < - data.frame(Mass = log(mass),Solv = log(solv_acc))

plotter< ; - (ggplot(df,aes(x = Mass,y = Solv))+ geom_point(shape = 1)+ stat_smooth(method =nls,formula = y〜i * x ^ z,start = list(i = 1,z = 0.2)))
绘图仪< - 绘图仪+实验室(x =质量kDa,y =溶剂可及性)
印刷(绘图仪)


$ b

运行上面的代码,我得到以下错误:

  pred $ fit:$ opera中的错误tor对于原子向量无效

我假设在尝试使用<$ c $时发生错误c> predict()?



在不使用ggplot2的情况下执行 nls 在同一个数据框中,我不会收到错误消息

 > nls1 = nls(Solv_i * Mass ^ z,start = list(i = 1,z = 0.2),data = df)
>预测(nls1)
[1] 7.893393 7.997985 8.115253 8.123230 8.234519 8.273135 8.295350 8.429871 8.468550 8.574147 8.583270 8.588134 8.647895 8.677831 8.692939 8.777944 8.781648 8.783757 8.796793 8.829609
[21] 8.860502 8.881445 8.890558 8.947512 8.994380 9.000995 9.001769 9.053953 9.161073 9.198919 9.213390 9.281841 9.303083 9.420894 9.430834 9.441670 9.442703

任何人都可以指出为什么我收到错误吗?

解决方案

您的问题在问题。简单地说,


根据predict.nls的文档,它无法为预测创建
标准错误,所以必须在
stat_smooth调用中关闭。 。

因此,我们需要关闭标准错误:

  ggplot(df,aes(x = Mass,y = Solv))+ 
stat_smooth(method =nls,formula = y〜i * x ^ z,se = FALSE ,
start = list(i = 1,z = 0.2))+
geom_point(shape = 1)


So Im using nls in ggplot2 to plot a power curve code is below:

mass <- c(4120,4740,5550,5610,6520,6870,7080,8500,8960,10350,10480,10550,11450,11930,12180,13690,13760,13800,14050,14700,15340,15790,15990,17300,18460,18630,18650,20050,23270,24530,25030,27540,28370,33460,33930,34450,34500)

solv_acc <- c(2760,2990,2990,3180,3900,4010,4140,4680,4750,5330,4980,5860,5930,5570,5910,6790,6690,7020,6240,6620,6600,6860,7940,7600,8250,8530,7410,9160,9140,10300,10440,10390,11020,12640,11920,12110,12650)

df <- data.frame(Mass=log(mass),Solv=log(solv_acc))

plotter <- (ggplot(df, aes(x=Mass, y=Solv)) + geom_point(shape=1) + stat_smooth(method = "nls", formula = y~i*x^z, start=list(i=1,z=0.2)))
plotter <-  plotter + labs(x = "Mass kDa" ,y = "Solvent Accessibility")
print(plotter)

Running the above code I get the following error:

Error in pred$fit : $ operator is invalid for atomic vectors

I am assuming the error occurs when it tries to use predict()?

When I perform nls without the use of ggplot2 on the same data frame I do not get an error

> nls1=nls(Solv~i*Mass^z,start=list(i=1,z=0.2),data=df)
> predict(nls1)
 [1] 7.893393 7.997985 8.115253 8.123230 8.234519 8.273135 8.295350 8.429871 8.468550 8.574147 8.583270 8.588134 8.647895 8.677831 8.692939 8.777944 8.781648 8.783757 8.796793 8.829609
[21] 8.860502 8.881445 8.890558 8.947512 8.994380 9.000995 9.001769 9.053953 9.161073 9.198919 9.213390 9.281841 9.303083 9.420894 9.430834 9.441670 9.442703

Can anyone point out why I am getting the error?

解决方案

Your question is answered in this question on the ggplot2 mailing list. Briefly,

According to the documentation for predict.nls, it is unable to create standard errors for the predictions, so that has to be turned off in the stat_smooth call. .

So, we need to turn off the standard errors:

ggplot(df, aes(x=Mass, y=Solv)) +
  stat_smooth(method="nls", formula=y~i*x^z, se=FALSE, 
              start=list(i=1,z=0.2)) +
  geom_point(shape=1) 

这篇关于在ggplot2中使用nls的pred $ fit错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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