为什么我的非线性回归曲线有直线段?

Why does my nonlinear regression curve have straight line segments?

我试着画了一条非线性曲线,但我不知道为什么它有直线段。

原数据如下:

ISIDOR <- structure(list(Pos_heliaphen = c("W30", "X41", "Y27", "Z24", 
                                       "Y27", "W30", "W30", "X41", "Y27", "W30", "X41", "Z40", "Z99"
), traitement = c("WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
                  "WW", "WW", "WW", "WW", "WW"), Variete = c("Isidor", "Isidor", 
                                                             "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", 
                                                             "Isidor", "Isidor", "Isidor", "Cali"), FTSW_apres_arros = c(0.462837958498518, 
                                                                                                                         0.400045032939416, 0.352560790392534, 0.377856799586057, 0.170933345859364, 
                                                                                                                         0.315689846065931, 0.116825600914318, 0.0332444780173884, 0.00966070114456602, 
                                                                                                                         0.0871102539376406, 0.0107280083093036, 0.195548432729584, 1), 
NLE = c(0.903498791068124, 0.954670066942938, 0.970762905436272, 
        0.873838605282389, 0.647875257025359, 0.53056603773585, 0.0384548155916796, 
        0.0470924009989314, 0.00403163281128882, 0.193696514297641, 
        0.0718450645564359, 0.295346695941639, 1)), class = c("tbl_df", 
                                                              "tbl", "data.frame"), row.names = c(NA, -13L))

这是代码:

mod = nls(NLE ~ 2/(1+exp(a*FTSW_apres_arros))-1,start = list(a=1),data = ISIDOR)
ISIDOR$pred = predict(mod,ISIDOR)
a = coef(mod)
RMSE = rmse(ISIDOR$NLE, ISIDOR$pred)
MSE = mse(ISIDOR$NLE, ISIDOR$pred)
Rsquared = summary(lm(ISIDOR$NLE~ ISIDOR$pred))$r.squared


  ggplot(ISIDOR,aes(x=FTSW_apres_arros)) + 
  geom_point(aes(y=NLE,color=Variete), pch=19, cex=3) +
  scale_color_manual(values = c("red3","blue3"))+
  geom_line(aes(y=pred), color="black", lwd=1.2) +
  scale_y_continuous(limits = c(0,1.0)) +
  scale_x_continuous(limits = c(0,1)) +
  labs(title = "Isidor", y="Expansion folliaire totale relative",x="FTSW",
       subtitle = paste0("y = 2/(1 + exp(",round(a,3), "* x)) -1)","\n",
                         "R^2 = ", round(Rsquared,3),"  RMSE = ", round(RMSE,3),"   MSE = ", round(MSE,3)))+
  theme(plot.title = element_text(hjust = 0,size=14, face = "bold", colour = "black"),
        plot.subtitle = element_text(hjust = 0,size=10, face = "italic", colour = "black"),
        legend.position = "none")

这是最后的数字:

你可以在曲线的尽头,有一条直线,不遵守图中计算的非线性方程。

您只是根据数据中已经存在的 x 值进行预测。由此产生的预测由直线连接起来。您需要向 predict 函数提供要预测的 x 值序列。如果你在 x 轴上均匀分布许多点,你会得到一条平滑的线。最好为此目的创建一个小的预测数据框:

pred_df <- data.frame(FTSW_apres_arros = seq(min(ISIDOR$FTSW_apres_arros), 
                                         max(ISIDOR$FTSW_apres_arros),
                                         length.out = 100))

pred_df$NLE <- predict(mod, newdata = pred_df)

现在我们将此数据框传递给 geom_line

data 参数
ggplot(ISIDOR, aes(FTSW_apres_arros, NLE)) + 
  geom_point(aes(color = Variete), pch = 19, cex = 3) +
  geom_line(data = pred_df) +
  scale_color_manual(values = c("red3","blue3"))+
  scale_y_continuous(limits = c(0, 1.0)) +
  scale_x_continuous(limits = c(0, 1)) +
  labs(title = "Isidor", 
       y = "Expansion folliaire totale relative",
       x = "FTSW",
       subtitle = paste0("y = 2/(1 + exp(", round(a, 3), "* x)) -1)","\n",
                         "R^2 = ", round(Rsquared, 3),"  RMSE = ",
                         round(RMSE, 3), "   MSE = ", round(MSE, 3)))+
  theme(plot.title = element_text(hjust = 0, size = 14, face = "bold", 
                                  colour = "black"),
        plot.subtitle = element_text(hjust = 0,size=10, face = "italic", 
                                     colour = "black"),
        legend.position = "none")