简体   繁体   中英

Mimic minimize function from Python in R

I have the following data points:

xdata looks like the following.

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 looks like the following.

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

I am running the following commands in 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)]

My main problem is being unable to track the stream of changes to the data. dataFit does the following operation:

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

where

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

Lastly, minimize is being called from the following library:

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

The results I get for Std in python are:

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

I am trying to replicate the same results in R or in Excel. Either one will be sufficient. The problem I am having is that I unable to accurately mimic the same behavior as the minimize (which is minimizing least-squares) and residual . I have tried searching for corresponding libraries in R with the minimize and residual function; however, I was not able to find any(nor use it correctly) that gave me the same results as in Python.

When I graph xdata , ydata , and the results of minimize (which I have provided above), I get the following graph in Python. Ultimately, I would just like to reproduce this same graph in R or Excel. 在此处输入图片说明

How to proceed? I am not an expert in Python, hence, I am not able to correctly port the code from Python to R or Excel.

You can replicate this in R using the function nls() . First, I set up your data so it can be read into 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))

I use your starting values for the parameters, although these are not the values that the model settles on. To see the parameters type nls.fit into the console and R will display information about the fitted model.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM