每次循环,R代码为方程式产生不同的输出

时间:2018-08-30 17:06:12

标签: r geometric-mean

我正在尝试计算几何平均值,并为某些参数找到最大可能值。我正在对两个分布进行卷积(我想,我不确定英语中的术语),其中之一是我方程的概率密度函数,并且想找到几何平均值最高的一组参数。我将尝试注释我的代码,以使我的工作更清晰。我这样做是为了确定某些情况下某个物种的最佳生态位宽度。适应性取决于物种特征与环境sdH的标准偏差与权衡函数之间的匹配。

但是,如果我更改了其中一个参数,即使我不希望这样做,它也会更改完整的输出。也许我忽略了一些错误,或者这是完全可以预料的,而我的期望是错误的,但是我处于机智。也许有人知道该怎么做?

deltax=0.5    #This is the step size for one of the distributions
alpha=c(1, 1.5, 2, 3, 4)    #These are one set of parameters I want to study. They are each a distinct case.
H=seq(-7, 7, deltax)    #This is the scope of the x-values for one distribution
sdH=seq(1, 7, 0.02)    #This is the scope of the standard deviation I want to study
optimumgvariance= matrix(0,ncol = length(alpha), nrow = length(sdH)) #a matrix to store my final values

这是我在以下代码中使用的第一组参数:

for (l in 1:length(sdH)){  #for each value of sdH I do the following:  
  g=seq(0.1, 9, 0.1)    # set up a vector of values, which determine the width of the other distribution
  gt=0    #initialize a helper variable
  tradeoff=numeric(length(g))     #initilaize a vector for the results of one of my equations
  expfitnessgeo=numeric(length(g))    #initiliaze a vector for the geometric mean
  optimumg=numeric(length(alpha))    #initialize a vector for the optimum value of g (the values from above) for the given set of parameters (depending on which loop we are in)
  for (k in 1:length(alpha)){    #for all values of alpha we do the following
    for (i in 1:length(g)){    #for all values of g
      gt=-(g[i]^2)    #I calculate the helper variabel gt (for clearer code futher below)
      tradeoff[i]=exp(gt/(2*alpha[k]^2))    #I calculate the results of the trade-off function for the combination of g and alpha in the current loop
      survival=numeric(length(H))    #I set up a vector to contain the results of the next equation
      for (j in 1:length(H)){    
        survival[j]=tradeoff[i]*exp(-((H[j])^2/(g[i]^2)))    #the survival
      }    
      expfitnessgeo[i]= exp(sum(log(survival)*dnorm(H, 0, sdH[l])*deltax)) # the maximum long term fitness can be identified by a maximum geometric mean of the probability density function    
    }    
    print(max(expfitnessgeo))    #just to control the output
    if(max(expfitnessgeo)>=0.1){    #the geometric mean needs to be larger than 0.1 
      optimumg[k]=g[which(expfitnessgeo==max(expfitnessgeo))]  #the optimal value of g is the one where the geometric mean of the pdf is maximal.  
    } else optimumg[k]=0    #if the geometric mean of the pdf is smaller than 0.1 I set the optimum g to zero

  }    
  optimumgvariance[l,] =optimumg #store the results in the matrix    
}    

这会产生预期的结果:

> tail(optimumgvariance)
       [,1] [,2] [,3] [,4] [,5]
[296,]    0    0  3.3  4.1  4.7
[297,]    0    0  3.3  4.1  4.7
[298,]    0    0  3.3  4.1  4.7
[299,]    0    0  3.3  4.1  4.7
[300,]    0    0  3.3  4.1  4.7
[301,]    0    0  3.3  4.1  4.7
> max(optimumgvariance[,2])
[1] 2.3
> max(optimumgvariance[,1])
[1] 1.5

但是,当我仅更改sdH=seq(1,10,0.02)的范围并保留所有其他内容时,结果将更改。我突然在第二列中有了值,之前我曾有过预期的零值:

deltax=0.5    
alpha=c(1, 1.5, 2, 3, 4)    
H=seq(-7, 7, deltax)    
sdH=seq(1, 10, 0.02)    
optimumgvariance= matrix(0,ncol = length(alpha), nrow = length(sdH))    

   > tail(optimumgvariance)    
       [,1] [,2] [,3] [,4] [,5]    
[446,]    0  2.9  3.4  4.1  4.8    
[447,]    0  2.9  3.4  4.1  4.8    
[448,]    0  2.9  3.4  4.1  4.8    
[449,]    0  2.9  3.4  4.1  4.8    
[450,]    0  2.9  3.4  4.1  4.8    
[451,]    0  2.9  3.4  4.1  4.8    
> max(optimumgvariance[,1])    
[1] 1.5    

我希望第2列中的值保持不变,但是不知道如何。而且,当我将sdH= seq(1,15, 0.02)放在第一列时,我什至会得到除零以外的其他值:

deltax=0.5
alpha=c(1, 1.5, 2, 3, 4)
H=seq(-7, 7, deltax)
sdH=seq(1, 15, 0.02)
optimumgvariance= matrix(0,ncol = length(alpha), nrow = length(sdH))

> tail(optimumgvariance)
       [,1] [,2] [,3] [,4] [,5]
[696,]  2.4    3  3.4  4.2  4.8
[697,]  2.4    3  3.4  4.2  4.8
[698,]  2.4    3  3.4  4.2  4.8
[699,]  2.4    3  3.4  4.2  4.8
[700,]  2.4    3  3.4  4.2  4.8
[701,]  2.4    3  3.4  4.2  4.8

有人有什么主意吗,这是什么原因造成的?我实际上想提高到sdH= seq(1,30, 0.02),但是当我不知道为什么会发生这种情况时,我不相信自己的结果。

0 个答案:

没有答案