在R函数

时间:2017-02-02 21:33:04

标签: r function for-loop plot

我写了一个函数,但是我想知道如何通过应用 seq(0, 1.5, by =.1)代替a来重复函数?

这是我的R代码:

n1 = 20
n2 = 20
T1 = -1.26491106 
df = 38
a = "wide" 
nb <- (n1*n2)/(n1+n2)
a = c(medium = 1/2, wide = sqrt(2)/2, verywide = 1)[[a]]


OO <- function(t,nb,df,a){

integrand <- function(g){

  (1+nb*g)^(-1/2)*(1+t^2/(df*(1+nb*g)))^(-(df+1)/2)*(2*pi)^(-1/2)*g^(-3/2)*exp(-a^2/(2*g))
}

num <- a*integrate(integrand,0,Inf)$value

denom <- (1+t^2/df)^(-(df+1)/2)

return(num/denom)
}

OO(t=T1,nb=nb,df=df,a=a)  ## HERE IS THE FUNCTION ##

1 个答案:

答案 0 :(得分:1)

不确定a上初始设置的含义是什么,但这里是如何在序列上应用函数,

  

sapply(seq(0.1,1.5,by = .1),函数(a)OO(t = T1,nb = nb,df = df,a = a))