简体   繁体   English

简化/压缩R中的长时间序列函数

[英]Simplifying/condensing long time series functions in R

I have a long time series loop that I would like to simplify/condense. 我有一个很长的时间序列循环,我想简化/压缩。 I am trying to simulate the calving of a cattle herd over a period of ten years (monthly intervals) using a random binomial distribution. 我正在尝试使用随机二项分布来模拟十年(每月间隔)期间牛群的产犊。 The function starts with the assumption that the cattle have been covered by the bull. 该功能从牛被牛覆盖的假设开始。 Each variable is affected by the previous. 每个变量都受前一个变量的影响。 The variables are as follows: 变量如下:

G1:G9 gestation for each month. 每月G1:G9妊娠。 MC1:MC7 mothers with calves for 7 months, then after the calves are weaned. MC1:MC7的母亲与小牛一起待了7个月,然后在小牛断奶之后。 Rest1:Rest6 periods of rest before they are covered by the bull again. Rest1:Rest6休息时间,然后再次被多头覆盖。 DeadCows based on the mortality rate. DeadCows基于死亡率。 NPreg non-pregnant cows based on the conception rate. 基于受孕率的NPreg非妊娠奶牛。

Inputs: size_cowherd, number of cattle in the herd. 输入:size_cowherd,畜群中的牛数。 concep, conception rate. 概念,受孕率。

Thanks in advance. 提前致谢。

The code I have is as follows: 我的代码如下:

size_cowherd<-100
concep<-0.95

cows <- function(t=119, mort=0.0005){
  G1<- numeric(length = t + 1)
  G2<- numeric(length = t + 1)
  G3<- numeric(length = t + 1)
  G4<- numeric(length = t + 1)
  G5<- numeric(length = t + 1)
  G6<- numeric(length = t + 1)
  G7<- numeric(length = t + 1)
  G8<- numeric(length = t + 1)
  G9<- numeric(length = t + 1)
  MC1<- numeric(length = t + 1)
  MC2<- numeric(length = t + 1)
  MC3<- numeric(length = t + 1)
  MC4<- numeric(length = t + 1)
  MC5<- numeric(length = t + 1)
  MC6<- numeric(length = t + 1)
  MC7<- numeric(length = t + 1)
  Rest1<- numeric(length = t + 1)
  Rest2<- numeric(length = t + 1)
  Rest3<- numeric(length = t + 1)
  Rest4<- numeric(length = t + 1)
  Rest5<- numeric(length = t + 1)
  Rest6<- numeric(length = t + 1)
  DeadCows <- numeric(length = t + 1)
  NPreg <- numeric(length = t + 1)

  G1[1]<- rbinom(1,size_cowherd,concep)
  G2[1]<- 0
  G3[1]<- 0
  G4[1]<- 0
  G5[1]<- 0
  G6[1]<- 0
  G7[1]<- 0
  G8[1]<- 0
  G9[1]<- 0
  MC1[1]<- 0
  MC2[1]<- 0
  MC3[1]<- 0
  MC4[1]<- 0
  MC5[1]<- 0
  MC6[1]<- 0
  MC7[1]<- 0
  Rest1[1]<-0
  Rest2[1]<-0
  Rest3[1]<-0
  Rest4[1]<-0
  Rest5[1]<-0
  Rest6[1]<-0
  DeadCows[1] <- 0
  NPreg[1] <- size_cowherd - G1[1] 

  for(step in 1:t){
    G2[step+1] <- rbinom(1, G1[step], (1-mort))
    G3[step+1] <- rbinom(1, G2[step], (1-mort))
    G4[step+1] <- rbinom(1, G3[step], (1-mort))
    G5[step+1] <- rbinom(1, G4[step], (1-mort))
    G6[step+1] <- rbinom(1, G5[step], (1-mort))
    G7[step+1] <- rbinom(1, G6[step], (1-mort))
    G8[step+1] <- rbinom(1, G7[step], (1-mort))
    G9[step+1] <- rbinom(1, G8[step], (1-mort))
    MC1[step+1] <- rbinom(1, G9[step], (1-mort))
    MC2[step+1] <- rbinom(1, MC1[step], (1-mort))
    MC3[step+1] <- rbinom(1, MC2[step], (1-mort))
   MC4[step+1] <- rbinom(1, MC3[step], (1-mort))
   MC5[step+1] <- rbinom(1, MC4[step], (1-mort))
   MC6[step+1] <- rbinom(1, MC5[step], (1-mort))
   MC7[step+1] <- rbinom(1, MC6[step], (1-mort))
   Rest1[step+1] <- rbinom(1,MC7[step],(1-mort))
   Rest2[step+1] <- rbinom(1,Rest1[step],(1-mort))
   Rest3[step+1] <- rbinom(1,Rest2[step],(1-mort))
   Rest4[step+1] <- rbinom(1,Rest3[step],(1-mort))
   Rest5[step+1] <- rbinom(1,Rest4[step],(1-mort))
   Rest6[step+1] <- rbinom(1,Rest5[step],(1-mort))
   G1[step+1] <- rbinom(1, Rest6[step], (1-mort))
   DeadCows[step+1] <-sum(G1[step]-G2[step+1],G2[step]-G3[step+1],G3[step]-
    G4[step+1],G4[step]-G5[step+1],G5[step]-G6[step+1],G6[step]-
    G7[step+1],G7[step]-G8[step+1],G8[step]-G9[step+1],G9[step]-
    MC1[step+1],MC1[step]-MC2[step+1],MC2[step]-MC3[step+1],MC3[step]-
    MC4[step+1],MC4[step]-MC5[step+1],MC5[step]-MC6[step+1],MC6[step]-
    MC7[step+1],MC7[step]-Rest1[step+1],Rest1[step]-
    Rest2[step+1],Rest2[step]-Rest3[step+1],Rest3[step]-
    Rest4[step+1],Rest4[step]-Rest5[step+1],Rest5[step]-
    Rest6[step+1],Rest6[step]-G1[step+1])

      if(G1[step]<size_cowherd){
        G1[step+1]<- rbinom(1,Rest6[step], concep)
        NPreg[step+1]<-Rest6[step]-G1[step+1]
      }
  }
  out <-cbind(G1,G2,G3,G4,G5,G6,G7,G8,G9,MC1,MC2,MC3,MC4,MC5,MC6,MC7,Rest1,R
  est2,Rest3,Rest4,Rest5,Rest6,DeadCows,NPreg)
  return(out)
 }

Below is a sample of what the output should look like. 下面是输出外观的示例。 In the 23rd month, the cycle restarts again. 在第23个月,周期再次开始。

    G1 G2 G3 G4 G5 G6 G7 G8 G9 MC1 MC2 MC3 MC4 MC5 MC6 MC7 Rest1 Rest2 Rest3 
     Rest4 Rest5 Rest6 DeadCows NPreg
1   96  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     4
2    0 96  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
3    0  0 96  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
4    0  0  0 96  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
5    0  0  0  0 96  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
6    0  0  0  0  0 96  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
7    0  0  0  0  0  0 96  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
8    0  0  0  0  0  0  0 96  0   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
9    0  0  0  0  0  0  0  0 96   0   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
10   0  0  0  0  0  0  0  0  0  96   0   0   0   0   0   0     0     0     0     
       0     0     0        0     0
11   0  0  0  0  0  0  0  0  0   0  96   0   0   0   0   0     0     0     0     
       0     0     0        0     0
12   0  0  0  0  0  0  0  0  0   0   0  96   0   0   0   0     0     0     0     
       0     0     0        0     0
13   0  0  0  0  0  0  0  0  0   0   0   0  96   0   0   0     0     0     0     
       0     0     0        0     0
14   0  0  0  0  0  0  0  0  0   0   0   0   0  96   0   0     0     0     0     
       0     0     0        0     0
15   0  0  0  0  0  0  0  0  0   0   0   0   0   0  96   0     0     0     0     
       0     0     0        0     0
16   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0  96     0     0     0     
       0     0     0        0     0
17   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0    96     0     0     
       0     0     0        0     0
18   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0    96     0     
       0     0     0        0     0
19   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0    95     
       0     0     0        1     0
20   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0    
      95     0     0        0     0
21   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0    95     0        0     0
22   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     
       0     0    94        1     0

Something like this should work for you. 这样的事情应该为您工作。 I think the trick here is to make use of matrices to keep the bookkeeping a bit more straightforward. 我认为这里的诀窍是利用矩阵来使簿记更加直接。

 size_cowherd <- 100

 concep <- 0.95

 stage_names <- c(paste0("G",seq(9)), paste0("MC",seq(7)), paste0("Rest",seq(6)))

 cows <- function(size_cowherd, concep, t=220, mort=0.0005, names=stage_names) {
   n_stages <- length(names)
   stages <- matrix(0, t, n_stages)
   dead_cows <- n_preg <- rep(NA, t)
   stages[1,1] <- rbinom(1, size_cowherd, concep)
   dead_cows[1] <- 0
   n_preg[1] <- size_cowherd - stages[1,1]
   for(tt in 2:t) {
     stages[tt,1] <- rbinom(1, stages[tt-1,n_stages], 1-mort)
     for(i in 2:n_stages) {
       stages[tt,i] <- rbinom(1, stages[tt-1,i-1], 1-mort)
     }
     dead_cows[tt] <- sum(stages[tt-1,] - stages[tt,c(2:n_stages,1)])
     if(stages[tt-1,1] < size_cowherd) {
       stages[tt, 1] <- rbinom(1, stages[tt-1,n_stages], concep)
       n_preg[tt] <- stages[tt-1,n_stages] - stages[tt,1]
     }
   }
   res <- cbind(stages, dead_cows, n_preg)
   colnames(res) <- c(names, "Dead", "N_Preg")
   return(res)
 }

 head(cows(100, 0.95), 24)

       G1 G2 G3 G4 G5 G6 G7 G8 G9 MC1 MC2 MC3 MC4 MC5 MC6 MC7 Rest1 Rest2 Rest3 Rest4 Rest5 Rest6 Dead N_Preg
  [1,] 96  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      4
  [2,]  0 96  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
  [3,]  0  0 96  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
  [4,]  0  0  0 96  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
  [5,]  0  0  0  0 96  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
  [6,]  0  0  0  0  0 96  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
  [7,]  0  0  0  0  0  0 95  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    1      0
  [8,]  0  0  0  0  0  0  0 95  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
  [9,]  0  0  0  0  0  0  0  0 95   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0
 [10,]  0  0  0  0  0  0  0  0  0  95   0   0   0   0   0   0     0     0     0     0     0     0    0      0
 [11,]  0  0  0  0  0  0  0  0  0   0  95   0   0   0   0   0     0     0     0     0     0     0    0      0
 [12,]  0  0  0  0  0  0  0  0  0   0   0  95   0   0   0   0     0     0     0     0     0     0    0      0
 [13,]  0  0  0  0  0  0  0  0  0   0   0   0  95   0   0   0     0     0     0     0     0     0    0      0
 [14,]  0  0  0  0  0  0  0  0  0   0   0   0   0  95   0   0     0     0     0     0     0     0    0      0
 [15,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0  95   0     0     0     0     0     0     0    0      0
 [16,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0  95     0     0     0     0     0     0    0      0
 [17,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0    94     0     0     0     0     0    1      0
 [18,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0    94     0     0     0     0    0      0
 [19,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0    94     0     0     0    0      0
 [20,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0    94     0     0    0      0
 [21,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0    94     0    0      0
 [22,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0    94    0      0
 [23,] 92  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      2
 [24,]  0 92  0  0  0  0  0  0  0   0   0   0   0   0   0   0     0     0     0     0     0     0    0      0  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM