从R到Python的转换,试图理解lin

2024-06-26 12:45:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个相当简单的问题。我已经将一些统计分析代码从R转换为Python。到目前为止,我一直做得很好,但我被困在这一行:

nlsfit <- nls(N~pnorm(m, mean=mean, sd=sd),data=data4fit,start=list(mean=mu, sd=sig), control=list(maxiter=100,warnOnly = TRUE))

从本质上说,该程序是计算非线性最小二乘拟合的一组数据,即“nls”命令。在原文中,“tilde”看起来像“enye”,我不确定这是否有意义。你知道吗

据我所知,Python中pnorm的等价物是标准.cdf从scipy.统计. 我想知道的是,“tilde/enye”在pnorm函数被调用之前做了什么“m”是一个预定义的变量,而“mean”和“sd”则不是。你知道吗

我还发现了一些代码,基本上是用Python复制nls:nls Python code,但是,由于发布日期(2013年),我想知道是否有更新的等价物,最好是用pyton3编写的。你知道吗

任何建议都会收到,谢谢!你知道吗


Tags: 代码datasdmeanstartcontrollistsig
2条回答

?nls可以看出:nsl中的第一个参数是formula

formula: a nonlinear model formula including variables and parameters. Will be coerced to a formula if necessary

现在,如果你做?formula,我们可以读到:

The models fit by, e.g., the lm and glm functions are specified in a compact symbolic form. The ~ operator is basic in the formation of such models. An expression of the form y ~ model is interpreted as a specification that the response y is modelled by a linear predictor specified symbolically by model

因此,在你的例子中~nls将左边的响应/相依/回归变量与非线性最小二乘法右边的回归/解释变量连接起来。你知道吗

最好的!你知道吗

这将最小化

sum((N - pnorm(m, mean=mean, sd=sd))^2)

使用在start中指定的meansd的起始值。它将执行最多100次迭代,在收敛前终止的情况下,它将返回而不是发出错误信号。你知道吗

nls的第一个参数是一个Rformula,它指定了回归,其中波浪号的左侧(N)是因变量,右侧是用于预测它的参数(meansd)和数据(m)的函数。你知道吗

注意,formula对象在R中没有固定的含义,但是每个函数都可以用它喜欢的任何方式来解释它们。例如,nls使用的formula对象的解释与lm使用的formula对象不同。在nls中,公式y ~ a + b * x将用于指定线性回归,但在lm中,相同的回归将表示为y ~ x。你知道吗

?pnorm?nls?nls.control?formula。你知道吗

相关问题 更多 >