【发布时间】:2020-06-13 19:35:43
【问题描述】:
我正在尝试使用predict() 填充值(行包含 x 值和 y 值作为NaN)来填充包含数据的图中的预测曲线。这个想法是获得比仅使用数据 x 值更平滑的预测曲线。但是,predict() 正在返回一些时髦的值,这些值似乎不是基于x 值的y 计算。问题是:
- 预测计算出了什么问题。
- 如何设置数据框或更改代码以获得平滑线。
这是产生可怕结果的代码:
library(ggplot2)
library(nlme)
# generate test data frame
x = c(0, 5, 100, 1000, 50, 200, 300, 500)
y = c(0, 3, 5, 6, NaN, NaN, NaN, NaN)
df=data.frame(x,y)
# a log model to fit the data
lF <- formula(y ~ Ymax-(Ymax-Y0)*exp(-k*x))
# nonlinear regression
model <- nls(lF, data=df,
start=list(Ymax=3.0, k=0.01, Y0=0.3),
na.action = na.omit)
# print out the model resutls
summary(model)
# Derive predicted lines
df$pred <- predict(model)
# plot the data and three models
ggplot(df, aes(x=x, y=y))+
geom_point() +
geom_line(aes(y=pred))
【问题讨论】:
标签: r ggplot2 model predict nls