簡體   English   中英

match.call() 有什么作用?

[英]What does match.call() do?

我正在嘗試將一些 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)
    # ...
}

這是在做什么? 我在match.call()的文檔中看到

match.call返回一個調用,其中所有指定的 arguments 都由它們的全名指定。

但我不明白這是什么意思。 在這種情況下,什么是“電話”? “參數由它們的全名指定”是什么意思?

最終,重要的部分是存儲在doseresp中的內容。 這些變量稍后會使用,所以我需要了解它們的值是什么,以便我可以在 Python 中執行類似的操作(可能使用 numpy、pandas 和 scipycipy)。

字面上的 R 答案在這里 但是您的問題意圖似乎是什么是慣用的 Python 等效於 R 的match.call() ,什么時候應該/不應該使用它? ,答案是:

  • (函數)introspection with inspect.signature(f) 1 , 2 :檢查哪個 function arguments 通過位置關聯與關鍵字/命名關聯(與默認值)匹配。 在 Python 函數/方法簽名中, func(arg_1, *args, **kwargs)大致相當於 R 的省略號...f(args, ...)中傳遞未指定的 args(通常繼承自super().func() )。
    • 永遠不要在生產代碼中使用自省
    • R 的范圍界定行為與 Python 不同,也可能存在范圍界定問題。 一般來說,在 Python 中創建一個 class (如果需要,可以自定義子類)並封裝對象的數據,以避免麻煩)會少一些悲傷。
  • 但是為什么你認為你需要將match.call()行移植到 Python 呢? 除了單元測試或調試您正在編寫的 class 之外,您通常不會在 Python 中執行此操作。 如果您移植drc::drm()以供自己使用,那么標准建議是實現您為自己的目的所需的絕對最小接口(而不是發布質量,並且您不會為此獲得報酬),並且忽略所有的花里胡哨。 您可能需要更長的時間才能弄清楚 R match.call()行在做什么,而不是忽略它或為您的用例拼湊它。
  • 實現重載 function 原型的 Pythonic 方法是將所有非必要參數默認為None ,然后任何 arg 解析邏輯為它們提供“智能默認”值(取決於其他 args 被傳遞/未被傳遞,或者對象的狀態)必須在 function 體內的 go 中。 這是可行的,Python 用戶應該了解您生成的代碼的作用。

至於你是否應該首先使用drc作為參考 package,我一個月前給你的建議相同, drc package 自 2016 年以來沒有 CRAN 版本,基本上處於休眠狀態,只有一兩個維護者,沒有郵件列表,也沒有詳細記錄 很可能還有其他 R 包具有更好的代碼或更好的文檔以用作參考。 我幾乎無法拼寫“bioassay”,所以我建議您在相關列表/用戶組(Python 和 R,學術和商業)上詢問從 ZEFE90A8E604A7C840E88D03A786 開始的參考建議。

(顯然,如果您真的想為Ze1e1D3D40573127E9EE0480CAF1283D6Z DOC和單位測試提供給drm維護者,以及使用ZA7F5F35426B927411FC927411fc9231b5631b56382173z的端口,但您可以提供類似的ZA7F5F35426B927411FC9311 IN GUTIFE。

(您的問題非常廣泛。我還嘗試通過評論來解決您的第二個更具體的問題。我不知道這是否會取代這一點,請通過編輯/評論進行更新。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM