【问题标题】:Mimic minimize function from Python in R在 R 中模仿 Python 的最小化函数
【发布时间】:2017-02-18 09:02:38
【问题描述】:

我有以下数据点:

xdata 如下所示。

1000.00
300.00
100.00
30.00
10.00
3.00
1.00
0.30
0.10
0.03
0.01
0.00

ydata 如下所示。

91.8
95.3
100
123
203
620
1210
1520
1510
1520
1590
1620

我在 python 中运行以下命令:

results = minimize(fit.dataFit,cParams,args=(xdata,np.array(ydata)))
curve = np.array(ydata)+results.residual
Std = [list(i) for i in zip(xdata,ydata, curve)]

我的主要问题是无法跟踪数据更改流。 dataFit 进行如下操作:

y_model = (ymax*xdata / (ec50 + xdata)) + Ns* xdata + ymin return y_model - ydata

在哪里

  1. ymax = 1624.75
  2. ymin = 91.85
  3. ec50 = 3
  4. Ns = 0.2045514

最后,从以下库调用最小化:

from lmfit import minimize,Minimizer,Parameters,Parameter,report_errors,report_fit

我在 python 中为Std 得到的结果是:

110
49.1
52.4
121
299
688
1110
1420
1550
1590
1610
1620

我正在尝试在 R 或 Excel 中复制相同的结果。任何一个都足够了。我遇到的问题是我无法准确地模仿与minimize(最小化最小二乘)和residual 相同的行为。我曾尝试使用minimizeresidual 函数在R 中搜索相应的库;但是,我找不到任何(也不能正确使用它)给我与 Python 相同的结果。

当我绘制xdataydataminimize(我在上面提供的)的结果时,我在 Python 中得到以下图表。最终,我只想在 R 或 Excel 中重现同样的图表。

如何进行?我不是 Python 专家,因此无法将代码从 Python 正确移植到 R 或 Excel。

【问题讨论】:

  • 查看R中的函数'nls'
  • 嗨@gabriel-f-geisler-mesevage,我很抱歉!我坚持纠正并消除我的错误假设。如您所料,列出的项目是 fit 函数的初始参数值。

标签: python r excel graph least-squares


【解决方案1】:

您可以使用函数nls() 在 R 中复制它。首先,我设置了您的数据,以便可以将其读入 R。

## Replicate results from Python `minimize` with R `nls()`
# First I load your data in
df <- data.frame(xdata = c(1000.00,300.00,100.00,30.00,10.00,3.00,1.00,0.30,
                           0.10,0.03,0.01,0.00),
                 ydata = c(91.8,95.3,100,123,203,620,1210,1520,1510,1520,1590,
                           1620))

# Now we estimate the model via nonlinear least squares
nls.fit <- nls(ydata ~ (ymax*xdata / (ec50 + xdata)) + Ns*xdata + ymin, data=df,
    start=list(ymax=1624.75, ymin = 91.85, ec50 = 3, Ns = 0.2045514))

我将您的初始值用于参数,尽管这些不是模型确定的值。要查看参数,请在控制台中输入nls.fit,R 将显示有关拟合模型的信息。

df$nls.pred <- fitted(nls.fit) # We extract the predicted values of the model
head(df) # We can examine the values of `xdata`, `ydata` and our predictions

  xdata ydata  nls.pred
1  1000  91.8 109.48985
2   300  95.3  49.02029
3   100 100.0  52.29715
4    30 123.0 120.61060
5    10 203.0 298.55367
6     3 620.0 687.63743

# We can see that the values we have obtained are very close to 
# what you obtained in the variable you named Std in python.

# I now load the ggplot2 library to recreate your plot
library(ggplot2)
ggplot(df, aes(xdata, ydata))+geom_point(color='red')+
  geom_line(data=df, aes(xdata, nls.pred))+
  theme_classic()+ # This makes the background black and white as in your plot 
  scale_x_log10() # The axis in your post is logged

【讨论】:

  • 亲爱的@Gabriel F Geisler Mesevage,非常感谢您的详细回复。它帮助我为我的研究目的创建了一个交互式标准曲线绘制 R Shiny 应用程序。 .我已经发布了一个全局曲线拟合相关问题,作为上述查询的下一步。 stackoverflow.com/questions/42465934/…你能告诉我如何解决这个问题并继续绘制曲线吗? - 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 2023-02-24
相关资源
最近更新 更多