2016-12-28 16 views
0

で機能を統合します。は、私は、以下の機能を持っているR

私はすべての単一の観察のために機能を統合することができました。

obs1<-integrate(Demandfunction_m1,3,16) 
obs2<-integrate(Demandfunction_m1,5,12) 
obs3<-integrate(Demandfunction_m1,4,18) 

...ので、私のデータセットは、260回の観測があり、calcualateする簡単な方法があれば、私は自分自身を尋ねた

上:それは(すべての観測は、統合の独自の限界を持っている)、このようになります。曲線の下の領域。

私は、このソリューションが見つかりました:私は私のR-Skriptにこれを適用しようとした

Integrate function with vector argument in R

surv <- function(x,score) exp(-0.0405*exp(score)*x) # probability of  survival 
area <- function(score) integrate(surv,lower=0,upper=Inf,score=score)$value 
v.area <- Vectorize(area) 

scores <- c(0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1) # 7 different scores 
v.area(scores) 
# [1] 14.976066 13.550905 12.261366 11.094542 10.038757 9.083443 8.219039 

を:

Demandfunction_m1 <- function(y) {y<- exp(coefm1[1,]+x*coefm1[2,]) 
return(y)} 
area <- function(x) integrate(Demandfunction_m1,lower=c(3,5,4),upper=c(16,12,18))$value 
v.area <- Vectorize(area) 

、その後、私は私がする必要があるかわかりません次へ

ご意見はありますか?

答えて

0

あなたのデマンド機能は意味をなさない。あなたは引数をyに渡していますが、計算には使用しません。

Demandfunction_m1 <- function(x,a,b) {exp(a+x*b)} 
area<-function(arg1, arg2, low, high) { integrate(Demandfunction_m1, lower=low, upper=high, a=arg1, b=arg2)$value } 
v.area<-Vectorize(area) 

lows<-c(3,5,4) 
highs<-c(16,12,18) 
result <- v.area(rep(coefm[1,],3), rep(coefm1[2,],3), lows, highs) 

#if you want to use different coefficients for a and b, just replace the repeated coef with a vector. 
result <- v.area(c(1,2,3), c(10,9,8), lows, highs) 
#equivalent to integrate(exp(1 + x*10), lower=3, upper=16)), integrate(exp(2 + x*9), lower=5, upper=12)), integrate(exp(3 + x*8), lower=4, upper=18)) 
関連する問題