- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试手动将一些 R 代码翻译成 Python,并遇到了以下代码片段:
"drm" <- function(
formula, curveid, pmodels, weights, data = NULL, subset, fct,
type = c("continuous", "binomial", "Poisson", "quantal", "event"), bcVal = NULL, bcAdd = 0,
start, na.action = na.omit, robust = "mean", logDose = NULL,
control = drmc(), lowerl = NULL, upperl = NULL, separate = FALSE,
pshifts = NULL)
{
## ... elided ...
## Storing call details
callDetail <- match.call()
## Handling the 'formula', 'curveid' and 'data' arguments
anName <- deparse(substitute(curveid)) # storing name for later use
if (length(anName) > 1) {anName <- anName[1]} # to circumvent the behaviour of 'substitute' in do.call("multdrc", ...)
if (nchar(anName) < 1) {anName <- "1"} # in case only one curve is analysed
mf <- match.call(expand.dots = FALSE)
nmf <- names(mf)
mnmf <- match(c("formula", "curveid", "data", "subset", "na.action", "weights"), nmf, 0)
mf[[1]] <- as.name("model.frame")
mf <- eval(mf[c(1,mnmf)], parent.frame()) #, globalenv())
mt <- attr(mf, "terms")
dose <- model.matrix(mt, mf)[,-c(1)] # with no intercept
resp <- model.response(mf, "numeric")
origDose <- dose
origResp <- resp # in case of transformation of the response
lenData <- length(resp)
numObs <- length(resp)
xDim <- ncol(as.matrix(dose))
varNames <- names(mf)[c(2, 1)]
varNames0 <- names(mf)
# only used once, but mf is overwritten later on
## Retrieving weights
wVec <- model.weights(mf)
if (is.null(wVec))
{
wVec <- rep(1, numObs)
}
## Finding indices for missing values
missingIndices <- attr(mf, "na.action")
if (is.null(missingIndices)) {removeMI <- function(x){x}} else {removeMI <- function(x){x[-missingIndices,]}}
## Handling "curveid" argument
assayNo <- model.extract(mf, "curveid")
if (is.null(assayNo)) # in case not supplied
{
assayNo <- rep(1, numObs)
}
uniqueNames <- unique(assayNo)
colOrder <- order(uniqueNames)
uniqueNames <- as.character(uniqueNames)
# ...
}
这是在做什么?我在the documentation for match.call()
中看到那个
match.call
returns a call in which all of the specified arguments are specified by their full names.
但是我不明白这是什么意思。在这种情况下,什么是“调用”? “参数由其全名指定”是什么意思?
最终,重要的部分是 dose
和 resp
中存储的内容。这些变量稍后会使用,因此我需要了解它们的值,以便我可以在 Python 中执行类似的操作(可能使用 numpy、pandas 和 scipy)。
最佳答案
R 的字面答案是 here 。但你的问题意图似乎是 What is the idiomatic Python equivalent of R's match.call()
, and when should I/not use it? ,答案是:
inspect.signature(f)
1 , 2 :检查哪些函数参数通过位置关联与关键字/命名关联(与默认值)匹配。在 Python 函数/方法签名中,func(arg_1, *args, **kwargs)
大致相当于 f(args) 中的 R 省略号 ...
, ...)
传递未指定的参数(通常继承自 super().func()
)。
match.call()
行移植到 Python?除了单元测试或调试类之外你正在编写,通常不会在 Python 中这样做。如果您要移植drc::drm()
供您自己使用,那么标准建议是实现您自己的目的所需的绝对最小接口(interface)(而不是发布质量,并且您不会为此获得报酬),并忽略所有花哨的东西。您可能需要更长的时间才能弄清楚 R match.call()
行正在做什么,而不是忽略它或将其混杂到您的用例中。None
,然后使用任何 arg 解析逻辑为它们提供“智能默认”值 (取决于传递/未传递的其他参数,或对象的状态)必须进入函数体内。这是可行的,Python 用户应该了解生成的代码的作用。至于您是否应该首先使用 drc
作为引用包,与我一个月前给您的建议相同,drc package has not had a CRAN release since 2016, is essentially dormant, only has one or two maintainers, no mailing-list, and isn't that well-documented 。很可能还有其他 R 软件包具有更好的代码或更好的文档可供引用。我几乎无法拼写“bioassay”,因此我建议您向相关列表/用户组(Python 和 R,学术和商业)询问从哪个引用包开始的建议。
(显然,如果你真的想向 drm
维护者贡献 R 文档和单元测试,以及做 Python 移植,你可以提供。但是如果你只想要一个基本的 Python 等价物。)
(您提出的问题非常广泛。我还尝试通过评论来解决您的第二个 much more specific reasking 。我不知道这是否会取代这个问题,请通过编辑/评论进行更新。)
关于python - match.call() 的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58239851/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!