2016-03-25 16 views
1

私は集団シミュレーションに関するStackoverflowに関するいくつかの質問を調べましたが、解決策を見つけることができません。ループの各サイクルの結果を保存

私の問題は、各ループの出力を保存しながら、1年目の最後の母集団データを2年目(ループ2)に持ち込む方法です。

読者が私のしていることを理解するのに役立つコードとコメントを記載しました。私はコードのいくつかが不要に見えることを理解していますが、この単純化された例でコードを残しました(コード全体にはより多くの年齢クラス、行列、死亡事象があります)。

sum_mat<-matrix(rep(0,3*3),nrow=3) # template for summer matrix 

onf=0       # Initial number of calves (hypothetical population) 
ony=250      # Initial number of yearlings 
ona2=500   # Initial number of cows 

cc<- c(0.46,0.33,0.16,0.36,0.42) #observed calf:cow ratios 

#nyears=10 

#for (i in 1:nyears) 
#{ 
# SUMMER 
pop0= c(onf,onf,onf, 
    ony,ony,ony, 
    ona2,ona2,ona2) # vector of age structure at the beginning of summer 

cc2=sample(cc,1) # sample from observed calfcow ratios for each loop/year 

cowsurv=rnorm(n=1,mean=0.1,sd=.05) #randomly select mortality rate for females 

sy_s= (1-(cowsurv)) # yearlings summer survival 
sa2_s=(1-(cowsurv)) # adult summer survival 

#leslie matrix for summer 
sum_mat[1,]=c(0,sy_s*cc2,sa2_s*cc2) #fecundity 
sum_mat[2,]=c(0,sy_s,0) 
sum_mat[3,]=c(0,0,sa2_s) 

demo_s=pop0*sum_mat      # Matrix transition process 

pop1=c(sum(demo_s[1,]),sum(demo_s[1,]),sum(demo_s[1,]), 
    sum(demo_s[2,]),sum(demo_s[2,]),sum(demo_s[2,]), 
    sum(demo_s[3,]),sum(demo_s[3,]),sum(demo_s[3,])) 

pop0<-c(pop0[1],pop0[4],pop0[7]) #extract N calves, yearlings, adults pre- summer 
pops<-c(pop1[1],pop1[4],pop1[7]) #extract N calves, yearlings, adults post-summer 
ccmod<-rep(cc2,3) #extract calfcow ratio 
age<-c('calf','1','2') #add age-class identifier 
stats<-cbind(age,pop0,pops,ccmod) #combine the extracted values 
stats<-as.data.frame(stats) 
#stats$year<-[i] #add simulation year 
write.csv(stats,"popmodel.csv",row.names=FALSE) 

#} 

####################################### 
######### year 2 ###################### 
####################################### 

onf=0      # no calves in new pre-summer year 
ony=pops[1]    #calves during post-summer are now yearlings 
ona2=pops[2]+pops[3]  #yearlings during post-summer now adults,added to existing summer adults 

# repeat above procedure for with new population, append each year to existing csv 

write.table(stats, file="popmodel.csv", append=T, row.names=F,col.names=F,sep=",") 

答えて

0

は、最初の年は、特に入力変数と出力ファイルを他のすべての年とは異なるルートをとり、条件付きforループを考えてみましょう。

# INITIALIZE VARIABLES 
sum_mat <- matrix(rep(0,3*3),nrow=3) # Template for summer matrix 
cc <- c(0.46,0.33,0.16,0.36,0.42)  # Observed calf:cow ratios 

nyears <- 10 

# LOOP THROUGH YEARS 
for (i in 1:nyears) 
{ 
    # CONDITION INPUT VARIABLES BY FIRST VS ALL OTHER YEARS 
    if (i == 1) { 
     onf <- 0      # Initial number of calves (hypothetical population) 
     ony <- 250     # Initial number of yearlings 
     ona2 <- 500     # Initial number of cows 

    } else { 
     onf <- 0      # No calves in new pre-summer year 
     ony <- pops[1]    # Calves during post-summer are now yearlings 
     ona2 <- pops[2]+pops[3]  # Yearlings during post-summer now adults,added to existing summer adults 
    } 

    # SUMMER 
    pop0 <- c(onf,onf,onf, 
      ony,ony,ony, 
      ona2,ona2,ona2)    # Vector of age structure at the beginning of summer 

    cc2 <- sample(cc,1)    # Sample from observed calfcow ratios for each loop/year 

    cowsurv=rnorm(n=1,mean=0.1,sd=.05) # Randomly select mortality rate for females 

    sy_s <- (1-(cowsurv))    # Yearlings summer survival 
    sa2_s <- (1-(cowsurv))    # Adult summer survival 

    # Leslie matrix for summer 
    sum_mat[1,] <- c(0,sy_s*cc2,sa2_s*cc2) # Fecundity 
    sum_mat[2,] <- c(0,sy_s,0) 
    sum_mat[3,] <- c(0,0,sa2_s) 

    demo_s <- pop0*sum_mat     # Matrix transition process 

    pop1 <- c(sum(demo_s[1,]),sum(demo_s[1,]),sum(demo_s[1,]), 
      sum(demo_s[2,]),sum(demo_s[2,]),sum(demo_s[2,]), 
      sum(demo_s[3,]),sum(demo_s[3,]),sum(demo_s[3,])) 

    pop0 <- c(pop0[1],pop0[4],pop0[7])  # Extract N calves, yearlings, adults pre-summer 
    pops <- c(pop1[1],pop1[4],pop1[7])  # Extract N calves, yearlings, adults post-summer 
    ccmod <- rep(cc2,3)      # Extract calfcow ratio 
    age <- c('calf','1','2')    # Add age-class identifier 
    stats <- cbind(age,pop0,pops,ccmod)  # Combine the extracted values 
    stats <- as.data.frame(stats)  

    stats$year <- i       # Add simulation year 

    # CONDITION OUTPUT BY FIRST VS ALL OTHER YEARS 
    if (i == 1) { 
     write.csv(stats,"popmodel.csv",row.names=FALSE) 
    } else { 
     write.table(stats, file="popmodel.csv", append=T, row.names=F,col.names=F,sep=",") 
    } 

} 
+0

ブリリアント!私はあなたのアプローチを覚えておく必要があります。ありがとうございました。 – ecologist55

関連する問題