我試圖從包中嵌入cramer函式。sjstats盡管該函式在自定義函式之外可以完美運行,但在其中卻無法正常運行。
非常感謝您提前。
library (sjstats)
cramer2 <- function(dta, x, y){
effsize <- cramer(x ~ y, data = dta)
return(effsize)
}
cramer2(x=gender, y=age, dta=df)
loglin 中的錯誤(資料,邊距,開始 = 開始,擬合 = 擬合,引數 = 引數,:falsche Spezifikationen für 'table' oder 'start'
uj5u.com熱心網友回復:
發生這種情況是因為x并且y不會在公式中自動替換您傳遞的變數。看:
f <- function(x, y) {
return(x ~ y)
}
f(a, b)
#> x ~ y
如果你想替換變數,你可以做類似的事情
f2 <- function(x, y) {
call("~", substitute(x), substitute(y))
}
f2(a, b)
#> a ~ b
所以在你的情況下,你可以這樣做:
library (sjstats)
cramer2 <- function(dta, x, y) {
f <- as.formula(call("~", substitute(x), substitute(y)))
effsize <- cramer(f, data = dta)
return(effsize)
}
顯然我們沒有您的資料,但使用內置資料集efc,我們可以證明它按預期作業:
data(efc)
cramer2(efc, e16sex, c161sex)
#> [1] 0.05258249
由reprex 包于 2022-02-27 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/434871.html
