用nls预测,总是return训练数据集的预测值

Prediction with nls, always return prediction values of train data set

我有4个训练集点,我用"nls"拟合训练集,然后预测包含2个点的测试集的响应。但是,"predict" 命令总是 returns 训练集的值。

The code is attached below:

##Following is train set
HD = c(714,715,716.6,717.6)
p_l = c(0.5,0.1,0.05826374, 0.005982334)
##Fitting with nls
raw_data = data.frame(HD,p_l)
exp_fit = nls(p_l~exp(a+b*HD),data = raw_data,trace = T,start = list(a = 0,b 
= 0))
##Following is test set
HD_test = c(718.2,719.17)
p_l_test = predict(exp_fit,newdata = HD_test)

您需要将数据框或命名列表作为 newdata 传递。

使用与原始预测变量相同的名称 (HD)。否则,newdata 被视为缺失,并返回原始训练数据的拟合值。

来自nls docs

newdata
A named list or data frame in which to look for variables with which to predict. If newdata is missing the fitted values at the original data points are returned.

HD_test = data.frame(HD = c(718.2,719.17)) # wrap in data frame, name "HD"
p_l_test = predict(exp_fit, newdata = HD_test)

p_l_test
[1] 0.0009320202 0.0002184518