我试图手动将一些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
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 ^{}, and when should I/not use it? ,答案是:
func(arg_1, *args, **kwargs)
是f(args, ...)
中R的省略号...
的粗略等价物,用于传递未指定的参数(通常从super().func()
继承)。match.call()
行移植到Python呢?除了在单元测试或调试您正在编写的类之外,通常不会在Python中这样做。如果你移植^{match.call()
行或在用例中混淆视听相比,弄清楚Rmatch.call()
行在做什么可能需要更长的时间。你知道吗None
,然后任何arg解析逻辑都必须进入函数体内部,以给它们提供“智能默认”值(取决于传递了/没有传递的其他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,学术和商业)上询问关于从哪个参考包开始的建议。你知道吗(显然,如果您真的想将R doc和单元测试贡献给
drm
维护人员,以及做Python端口,您可以提供。但如果您只想要一个基本的Python等价物,那听起来太悲伤了。)(这个问题问得很广泛。我还试着用一个评论来回答你的第二个much more specific reasking。我不知道这是否会取代这个,请通过编辑/评论更新。)
相关问题 更多 >
编程相关推荐