如何计算非线性回归"y=2/(1+exp(a*x))-1"中的系数"a"?还要画图?

How to calculate the coefficient "a" in a non-linear regression "y=2/(1+exp(a*x))-1"? And also draw the graph?

如题中所述,给定x的某个值(FTSW),其对应的值为 y (NLE),用方程 y=2/(1+exp(a*x))-1,我如何计算“a”并绘制图?

structure(list(FTSW = c(0.900359288405312, 0.900334904035704, 
                        0.905841413662097, 0.667621859862976, 0.632473107345453, 0.462837958498518, 
                        0.400045032939416, 0.352560790392534, 0.377856799586057, 0.628289954726284, 
                        0.311242672407523, 0.170933345859364, 0.311242672407523, 0.315689846065931, 
                        0.14105342778594, 0.147112396871317, 0.0722352096264757, 0.0615716495856242, 
                        0.396997072279772, 0.0332444780173884, 0.0871102539376406, 0.320745031679664, 
                        0.232853736349989, 0.195548432729584), NLE = c(0.557811005824568, 
                                                                       0.947807189426057, 0.676214815603592, 0.444390402075227, 0.97602256699577, 
                                                                       0.728254923413566, 0.306279885240675, 0.970762905436273, 0.611065629690272, 
                                                                       0.253424268258028, 0.557708161582854, 0.108086143843963, 0.727886237647626, 
                                                                       0.53056603773585, 0.54236648424231, 0.338367448826758, 0.111826531578449, 
                                                                       0.231650365670985, 0.409424366533147, 0.047092400998931, 0.19369651429764, 
                                                                       0.342990129203114, 0.288499226405363, 0.295346695941639)), class = c("tbl_df", 
                                                                                                                                            "tbl", "data.frame"), row.names = c(NA, -24L))

我试过了:

  df <- read_excel("##.xlsx")
  x <- FTSW
  y <- NLE

  ggplot(df, aes(x, y))+
  geom_point(pch=19, cex=3, ,col="Blue3") +
  geom_smooth(method="nls", formula=y ~ 2/(1+exp(a*x))-1, method.args=list(start = list(a=1)), se=FALSE,lwd= 1.2, col= "black")+
  scale_y_continuous(limits = c(0,1.0)) +
  scale_x_continuous(limits = c(0,1)) +
  labs(title ="Blancs",y="Expansion folliaire verte relative",x="FTSW")+
  theme_update(plot.title=element_text(hjust=0.5))

有了这个,我可以得到图表,但我不知道如何获得“a”值。谁能帮帮我?

您可以使用 stat::nls 单独估计模型(例如 mod),并通过 coef(mod)

获得系数
mod = nls(NLE ~ 2/(1+exp(a*FTSW))-1,start = list(a=1),data = dat)
dat$pred = predict(mod,dat)
a = coef(mod)

ggplot(dat,aes(x=FTSW)) + 
  geom_point(aes(y=NLE), pch=19, cex=3, ,col="Blue3") +
  geom_line(aes(y=pred), color="black", lwd=1.2) + 
  labs(caption=paste("'a' value from model=",round(a,4))) + 
  scale_y_continuous(limits = c(0,1.0)) +
  scale_x_continuous(limits = c(0,1)) +
  labs(title ="Blancs",y="Expansion folliaire verte relative",x="FTSW")+
  theme_update(plot.title=element_text(hjust=0.5))

向 nls 添加一个跟踪,它将 运行 它的结果存储在全局环境中的 .nls 中。

trace(nls, exit = quote(.nls <<- returnValue()))
# run ggplot
untrace(nls)

coef(.nls)
##         a 
## -2.974184