“简化”人口模型的R代码

时间:2018-02-12 22:40:42

标签: r

我想知道是否有可能帮助简化我为uni课程所做的一些代码。在过去的一周里,我已经基本上被R抛入深处(所以知之甚少),并想知道是否有一种非常明显的方法可以简化这一点,所以它不是那么笨重!

我正在计算岩岸上一群藤壶的沉降率(按照Hines 1979)。我有四个不同的结算率我的三个物种的脚本运行并没有问题,我只是想知道我怎么能把它连接起来。脚本如下:

#  Roughgarden et al 1985
#  Six age classes.  Data from Roughgardenetal1985_1Species.xls
# Population projection matrix
############################### C.FISSUS #####################################
#1.0
A <- matrix(c(0.8609,   1.4062, 1.9515, 2.4957, 2.6825, 2.8339,
          0.1522,   0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
          0.0000,   0.2378, 0.0000, 0.0000, 0.0000, 0.0000,
          0.0000,   0.0000, 0.1000, 0.0000, 0.0000, 0.0000,
          0.0000,   0.0000, 0.0000, 0.1000, 0.0000, 0.0000,
          0.0000,   0.0000, 0.0000, 0.0000, 0.1000, 0.0000
), nrow=6, byrow=TRUE)

par(mfrow=c(2,4))
# Starting population vector
N0 <- matrix(c(0, 0, 0, 0, 0, 0), nrow=6, byrow=TRUE)

# Settlement per unit free space (per cm2 / 100 = per mm2), for each species     use: 1.0, 0.1, 0.01, and 0.001
s <-1.0

# Area occupied by age classes (mm2)
Ax <- matrix(c(2.33,9.45,15.15,18.78,20.92,22.14), nrow=6, byrow=TRUE)

# Set up  matrix to store population stage (rows) structure over time (cols)
Nt<-matrix(data=0, ncol=50, nrow=6)       # Create a vector to store results
Nt[,1]<-N0                                # Make the first element (col 1)     equal to N0 

for (step in 1:49) {                      # Step through time, calculating         Nt+1 each time
  Nt[,step+1]<- A %*% Nt[,step]           # Apply mortality
  AreaOfBarnacles <- Ax * Nt[,step+1]     # Calculate area occupied by     surviving barnacles 
  Ft <- max(100 - sum(AreaOfBarnacles),0) # Calculate free space
  print(sum(AreaOfBarnacles))
  Nt[1,step+1] <- s * Ft                  # Number of new recruits
}
#Nt

# Transpose Nt for plotting

TNt <- t(Nt)
matplot(TNt, xlab = "Time, t", ylab = "Population Size, Nt", type="l", main = "Chthamalus fissus")
title(main="s = 1.0", line = 0.5)

我基本上需要运行这部分脚本共12次。这三种物种各有四次(每次都会改变s值(1次,0.1次,0.01次和0.001次)。我想尝试制作它,这样我就可以添加一点,就像“运行这个在这四种不同的结算率下编写脚本,并每次生成四张图表“所以我只需要将这部分脚本重复三次(每个物种一次)。但是,我似乎无法让它工作并最终做得很久!

非常感谢您花时间阅读这个冗长的问题,就像我说的那样,我对R(以及一般的编码)非常陌生,所以如果我要问的是愚蠢的话,我会道歉!

P.S。 (奖金回合?) 如何在不妨碍这些图形的情况下为这些图形添加图例?有没有办法让一个传说成为自己的图像,这样它就不会覆盖我的图形?

1 个答案:

答案 0 :(得分:1)

您可以将操作包装到一个函数中:

## Defining the function
population.projection <- function(settlement, matrix_A, area_occupied) {

    # Starting population vector
    N0 <- matrix(c(0, 0, 0, 0, 0, 0), nrow=6, byrow=TRUE)

    # Set up  matrix to store population stage (rows) structure over time (cols)
    Nt<-matrix(data=0, ncol=50, nrow=6)       # Create a vector to store results
    Nt[,1]<-N0                                # Make the first element (col 1)     equal to N0 

    for (step in 1:49) {                      # Step through time, calculating         Nt+1 each time
        Nt[,step+1]<- matrix_A %*% Nt[,step]           # Apply mortality
        AreaOfBarnacles <- area_occupied * Nt[,step+1]     # Calculate area occupied by     surviving barnacles 
        Ft <- max(100 - sum(AreaOfBarnacles),0) # Calculate free space
        # print(sum(AreaOfBarnacles))
        Nt[1,step+1] <- settlement * Ft                  # Number of new recruits
    }

    # Transpose Nt for plotting
    return(t(Nt))
}

此功能会将s变量和A以及Ax两个矩阵重命名为settlementmatrix_Aarea_occupied。 -explanatory。

然后您可以输入数据:

## matrix_A input
matrix_A<- matrix(c(0.8609,   1.4062, 1.9515, 2.4957, 2.6825, 2.8339,
          0.1522,   0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
          0.0000,   0.2378, 0.0000, 0.0000, 0.0000, 0.0000,
          0.0000,   0.0000, 0.1000, 0.0000, 0.0000, 0.0000,
          0.0000,   0.0000, 0.0000, 0.1000, 0.0000, 0.0000,
          0.0000,   0.0000, 0.0000, 0.0000, 0.1000, 0.0000
), nrow=6, byrow=TRUE)

## Area occupied by age classes (mm2)
area_occupied <- matrix(c(2.33,9.45,15.15,18.78,20.92,22.14), nrow=6, byrow=TRUE)

## Setting the s values
my_settlement_values <- c(1, 0.1, 0.01, 0.001)

循环显示结算值以绘制结果:

## Setting the graphic parameters
par(mfrow=c(2,2))

## Looping through the s values
for(one_settlement in my_settlement_values) {
    ## Plotting the results
    matplot(population.projection(settlement = one_settlement, matrix_A, area_occupied), xlab = "Time, t", ylab = "Population Size, Nt", type="l", main = "Chthamalus fissus")
    ## Adding the title
    title(main = paste("s =", one_settlement), line = 0.5)
}