r中

2016-12-28 22 views
0

集成一個功能,我有以下功能:r中

Demandfunction_m1 <- function(y) {y<- exp(coefm1[1,]+x*coefm1[2,]) 
    return(y)} 

在下一步我想在我的數據集中的每個觀測計算曲線下面積。

我能夠整合每一個觀察的功能。它看起來像這樣(每個觀察有其自身集成的限制):

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

...等等

我的數據集有260個觀察,我問自己,如果有到calcualate了一個更簡單的方法曲線下的區域。

我發現這個解決方案:

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 

我試圖將其應用到我的R-Skript:

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))