2017-06-20 18 views
1

私は自分の機能をどうすれば作れるのだろうかと思っていますBpp to 最初の引数のベクトルを受け入れますt`integrate()`を使ってR関数のベクトルを受け入れる方法は?

Bpp = function(t, n1, n2 = NULL){ 

     N = ifelse(is.null(n2), n1, n1*n2/(n1+n2)) 
    df = ifelse(is.null(n2), n1 - 1, n1 + n2 - 2) 

    H1 = integrate(function(delta)dcauchy(delta, 0, sqrt(2)/2)*dt(t, df, delta*sqrt(N)), -Inf, Inf)[[1]] 
    H0 = dt(t, df) 
    BF10 = H1/H0 
p.value = 2*(1-pt(abs(t), df)) 

list(BF10 = BF10, p.value = p.value) 
} 

Bpp(t = -6:6, 20, 20) ## This will give error because `t` is now a vector? 
+0

'Vectorize'は、これらの状況では、一般的に有用です。ベクトルを直接受け入れるように書き換える方が良いですが。 – Dason

答えて

2

私はテストなしで簡単な答えを与えることができるように見えます。あなたのBppに次のように使用します。

# joint density 
joint <- function(delta, t) dcauchy(delta, 0, sqrt(2)/2) * dt(t, df, delta*sqrt(N)) 
# marginal density of `t` 
marginal.t <- function (t) integrate(joint, lower = -Inf, upper = Inf, t = t)[[1]] 
H1 <- sapply(t, marginal.t) 

ので、ここではまた、どのようにそれは次のようになりVectorizeを使用することができますか?

Bppを使用します。

Bpp <- Vectorize(Bpp, vectorize.args = "t") 
Bpp(-6:6, 20, 20) 
関連する問題