微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 method="nls" 绘制 3 参数威布尔曲线

如何解决使用 method="nls" 绘制 3 参数威布尔曲线

我想用函数 y ~ mexp(-1(x/b)^c) 绘制几条 3 参数 Weibull 曲线。 在大多数情况下,我可以定义起始参数并使用 nls2 和 ggplot2 绘制曲线。但是,我未能在 ggplot2 中绘制一条特定曲线(不过我可以使用 nls2 拟合曲线)。

method="nls" 中使用 ggplot 命令对我很重要(而不是 loess,后者在许多情况下往往会过度拟合曲线)。 有人可以帮我解决这个问题吗?非常感谢!

    #Load packages
library(ggplot2)
library(nlme)
library(nls2)
# library(proto)

#model structure: 3 parameter Weibull
#y ~ m*exp(-1*(x/b)^c)

dip3dta<-structure(list(ploidy = structure(c(1L,1L,1L),.Label = c("dip","trp"),class = "factor"),geno = structure(c(3L,3L,3L),.Label = c("dip1","dip2","dip3","dip4","dip5","dip6","dip7","dip8","trp1","trp2","trp3","trp4","trp5","trp6","trp7","trp8"),Photo = c(10.03907124,16.04016877,5.799933798,6.256058037,1.34916505,9.609508391,12.84023945,8.436093321,7.732332332,15.38729611,2.157795186,5.93553951,3.37322132),WBPhoto = c(11.77970983,13.52705488,7.585920181,6.118582453,2.570461685,10.80358492,9.445462376,5.386306724,5.840252952,15.84494637,3.60398487,9.32456564,3.437440219),PDLWP = c(3.1,2.6,5.8,7.7,19,3.5,4.25,9,8.16,2.25,13.92,4.33,14.58),Treatment = structure(c(1L,.Label = "DC",class = "factor")),row.names = 27:39,class = "data.frame")

#Define the outer boundaries to search for initial values
Grddip3 <- data.frame(m=c(0,45),b=c(0,8),cc=c(0,0.6))

#Brute-force initial values
fitdip3 <- nls2(Photo ~ m*exp(-1*(PDLWP/b)^cc),data=dip3dta,start = Grddip3,algorithm = "brute-force")
fitdip3
finalfitdip3 <- nls(Photo ~ m*exp(-1*(PDLWP/b)^cc),start=as.list(coef(fitdip3)),algorithm = "port",lower = coef(fitdip3)/1000,control=list(maxiter=1000)) 

上面的代码工作正常,但我无法使用 method="nls"

在 ggplot 中绘制曲线
# Plot in ggplot
exampledip3<-ggplot(dip3dta,aes(x=PDLWP,y=Photo)) + geom_point(size=2)+
  theme_classic()+
  theme(legend.position="none")+
  theme(axis.text=element_text(size=18),axis.title=element_text(size=17,),axis.title.y=element_text(margin=margin(0,20,0)))+
  stat_smooth(method = "nls",formula = y ~ m*exp(-1*(x/b)^c),size = 0.9,se = FALSE,colour = "black")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。