2014-05-14 2 views
0

내 변수의 시작 값이 단일 관찰 대신 다른 시간에 계산되는 벡터 인 시뮬레이션 세트를 작성하는 데 문제가 있습니다. . 시작점을 변경하여 하나씩 시뮬레이션을 실행 한 다음 모든 결과를 다른 행렬로 다시 그룹화 할 수 있습니다. 그러나 나는 약 2000 건의 사례를 다루고 있기 때문에보다 우아하고 빠른 방법이 필요하다.for 루프를 사용하여 다른 점에서의 벡터 시뮬레이션 R

이 내가 사용하고있는 실제 코드 만 같은 문제의 예 아니다, 이제 가정 해 봅시다 :

벡터와
time<-c(65,130,195,260) #in days 

simulation<-matrix(numeric(10*4),10,4) 

    #vessel matrix containing the desired number of simulations for 1 initial case 

results<-NULL 

initial<-.5 

    #This would be one case and I've got a vector with approx.2000 

    #loop 

for(i in 1:10){ 
    simulation[i,] = (initial*exp(-a*(time/260)) + 
        b*(1-exp(-a*(time/260))) + 
        c*(j/260))*rnorm(1) 
} 

results<-colMeans(simulation) 


    # With this I end up with a row vector of 4 entries containing the average of 
    # the simulations for the first case at four dates. 

당신은 대체 얼마나 initial, 내가 끝낼 수있는 곳의이 initial=seq(from=0, to=10, by=.5)을 가정 해 봅시다 각 행에는 여전히 각 날짜에 대해 평균 10 개의 시뮬레이션이 포함 된 20X4 행렬?

+0

'a','b','c' 및'j'는 무엇입니까? 그들은 상수입니까? 'for' 루프를 실행할 때마다 똑같은 것을 얻지 않겠습니까? – nograpes

+2

@nograpes 거기에'rnorm (1)'이 숨어있다. – josliber

답변

0

따라서 for 루프 대신 replicate을 사용하여 함수에 넣으면 완료됩니다. 당신은 벡터 또는 목록을 통해 코드를 사용하려면

time<-c(65,130,195,260) 
initial<-.5 

# I assume a,b,c,j are constants 
a<-b<-c<-j<-1 

# Use replicate instead of a four loop to generate your matrix directly: 
mat<-replicate(10,(initial*exp(-a*(time/260)) + b*(1-exp(-a*(time/260))) + c*(j/260))*rnorm(1)) 
# Get the means for all four variables (note matrix is transposed) 
rowMeans(mat) 

# Wrap this in a function: 
f<-function(initial) { 
    mat<-replicate(10,(initial*exp(-a*(time/260)) + b*(1-exp(-a*(time/260))) + c*(j/260))*rnorm(1)) 
    rowMeans(mat) 
} 

# Now repeat for a variety of "initial" 
sapply(seq(from=0, to=10, by=.5),f) 

# [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8] 
# [1,] 0.04131179 -0.1703167 0.2165282 0.4518704 -0.4305232 1.0390553 -0.09262142 -0.3767832 
# [2,] 0.07293559 -0.1941923 0.2165282 0.4239343 -0.3889186 0.9154405 -0.08016291 -0.3217918 
# [3,] 0.09756422 -0.2127866 0.2165282 0.4021776 -0.3565170 0.8191691 -0.07046022 -0.2789645 
# [4,] 0.11674502 -0.2272679 0.2165282 0.3852335 -0.3312825 0.7441930 -0.06290376 -0.2456105 
+0

실제로'for' 대신에'replicate'을 사용하고 모든 것을 함수로 싸는 것이 빠릅니다. 'a','b','c','j'는 상수이고, 나는 그것을 포함하는 것을 잊었습니다. –

0
time<-c(65,130,195,260) #in days 

simulation<-matrix(numeric(10*4),10,4) 
#vessel matrix containing the desired number of simulations for 1 initial case 

results<-NULL 

#initial<-.5 

initial=seq(from=0.5, to=10, by=.5) # Changed This to be vector 


#This would be one case and I've got a vector with approx.2000 

#loop 
## Writing to a function to help in reusing the code 
MM <- function(initial){ 

for(i in 1:10){ 
    simulation[i,] = (initial*exp(-10*(time/260)) + 
         20*(1-exp(-10*(time/260))) + 
         20*(1000/260))*rnorm(1) 
} 

results<-colMeans(simulation) 
return(results) 
} 

sapply(initial, FUN=MM) # This prints the colmeans for each inputted element in the vector initial 

#Output: 
#     [,1]   [,2]   [,3]   [,4] 
#[1,] 0.04560954756720 -7.358191844904 -8.098682864513 3.665639204753 
#[2,] 0.04631255769454 -7.468652842648 -8.217009769192 3.717726620815 
#[3,] 0.04637026427987 -7.477720033496 -8.226722633000 3.722002216291 
#[4,] 0.04637500112485 -7.478464313845 -8.227519913413 3.722353178540 
#    [,5]   [,6]   [,7]   [,8] 
#[1,] 18.98057953410 21.03382334801 11.96959043580 63.54595548535 
#[2,] 19.24268217595 21.31585924907 12.12529951449 64.34721302134 
#[3,] 19.26419687094 21.33901016563 12.13808089400 64.41298424507 
#[4,] 19.26596290465 21.34091050858 12.13913005352 64.41838307588 
#    [,9]   [,10]   [,11]   [,12] 
#[1,] -20.01917307564 12.56144497430 27.31607000721 -17.70877647896 
#[2,] -20.26360373127 12.70980687528 27.62780928081 -17.90382189201 
#[3,] -20.28366782130 12.72198516172 27.65339839866 -17.91983219447 
#[4,] -20.28531478210 12.72298481635 27.65549888136 -17.92114640013 
#    [,13]   [,14]   [,15]   [,16] 
#[1,] -1.163127428493 -13.07282052205 20.91363938663 -15.43763356270 
#[2,] -1.175475358083 -13.20640615074 21.11903955938 -15.58312496123 
#[3,] -1.176488937866 -13.21737152689 21.13589983227 -15.59506762248 
#[4,] -1.176572137562 -13.21827161978 21.13728380775 -15.59604793581 
#    [,17]   [,18]   [,19]   [,20] 
#[1,] 4.129982905656 -32.29365824471 -26.80599719664 -13.92279757936 
#[2,] 4.167268037791 -32.57240758567 -27.02676696018 -14.03195652202 
#[3,] 4.170328587811 -32.59528872494 -27.04488884592 -14.04091683368 
#[4,] 4.170579813055 -32.59716692322 -27.04637638088 -14.04165234085 

당신은 기능의 가족을 적용 사용해야합니다. 희망이 도움이!

+0

도움이 되셨습니다. 고마워요. 함수를 정의한 다음 변경하는 벡터에 적용하는 것이 더 편리합니다. 이제 매개 변수를 무기한 변경하고 다른 벡터 세트를 사용할 수 있습니다. –

관련 문제