【问题标题】:How to take the last value of for loop iteration in R?如何在R中获取for循环迭代的最后一个值?
【发布时间】:2015-03-24 12:42:27
【问题描述】:

有人知道如何在 for 循环中提取最后的交互值吗?例如,我有一个连续的迭代

94.53630、94.53630、94.53630、99.57083、101.29593、101.46015、101.46150、101.46150、101.46150、101.46150。这是在每个执行步骤中,但我只想提取最后一个(101.46150)。因为,我正在运行我的 for 循环 9 次,并且我正在查看迭代 9 个值的结束。在这个 R 脚本中,我想在第一次迭代后只取一个具有相应概率值的 x.new。

# | ------------------------------------------------------------------------------------------------------------------------------------------
# | The CDF and PDF functions for product models. My original data is well fitted to Persoan Type-3
# | From the diffination  of Quantiles we set our F(x) and derivatives of F(x) as follow:
# | Q(p)= { x:Pr(X<=x)=p } or equivalently ;  Q(p)= { x:Pr(X<=x)- p =0 } --------- > (1)
# | CDF1 = F1(x) ---------------------------------------------------------------- >> (2) 
# | CDF2 = F2(x) ---------------------------------------------------------------- >> (3) 
# | PDF1 = f1(x) ---------------------------------------------------------------- >> (4)
# | PDF2 = f2(x) ---------------------------------------------------------------- >> (5)
# | Using the above five  model equations I  want to calculate quantils for the given probability values.
# | This lead Us to Newton-Raphson algorithm ;(Newton Method leads to the recurrence)  
# | # | Qx+1 = X[k]- F(x)-prob/F'(x) ------------------------------------------------- >>> (6)
# | Where ;;
# | F(x)  = F1(x) *F2(x) - prob = 0 ,,,,,  the CDF function -------------------- >>> (7)
# | F'(x) = f1(x)*F2(x) + f2(x)*F1(x) ,,,,, the PDF function ------------------ >>>> (8)
# | prob=c(0.5,0.65,0.70,0.75,0.80,0.85,0.90,0.95,0.998,0.999)
# | 
# | -----------------------------------------------------------------------------------------------------------------------------------------
rm(list=ls())
Sys.setenv(LANGUAGE="en")  # to set languege from Polish to English
setwd("C:/Users/sdebele/Desktop/From_oldcomp/Old_Computer/Seasonal_APP/Data/Data_Winter&Summer")
# | -----------------------------------------------------------------------------------------------------------------------
# | --------------------------------------------------------------------------------->
# | --------------------------------------------------------------------------------->
# | -------------------------------------------------------------------------------------------------------------------------
Fx=function(x) # Equation (7) # ! Evaluate function at old estimate
{

  require(PearsonDS)
  return(ppearsonIII(x,shape=3.743843 ,location= 0,scale= 7.976308)*
           ppearsonIII(x,shape=1.492779  ,location=3.295901, scale=9.212522));
}
dFx=function(x) # Equation (8) # ! Evaluate derivative at old estimate
{
  require(PearsonDS) 
  return((dpearsonIII(x,shape=3.743843 ,location= 0,scale= 7.976308))*
           (ppearsonIII(x,shape=1.492779  ,location=3.295901, scale=9.212522)) +
           (dpearsonIII(x,shape=1.492779  ,location=3.295901, scale=9.212522))*
           ppearsonIII(x,shape=3.743843 ,location= 0,scale= 7.976308));
}
# | ------------------------------------------------------------------------------------------------------------------------------------
# |Defining Parameters for Newton-Raphson algorithm and while loop 
# | 
# | --------------------------------------------------------------------------------------------------------------------------------------
prob=c(0.5,0.65,0.75,0.80,0.85,0.90,0.95,0.998,0.999)
par(mfrow=c(1,2))
par("lwd"=2)
curve(dFx,from=3,to=70,col="red",lwd=2);
curve(Fx,from=3,to=70,col="blue",lwd=2);
start<-locator(n=1)$x;
col=rainbow(20)
x.new<-NULL;
x.new<-cbind(x.new,start);
n=1;
niter=1 ; #  ! Number of iterations
niter_max = 100; #  ! Maximum of iterations allowed
counter=1
# | --------------------------------------------------------------------------------------------------------------------------
# | Here we start calculating quantiles 
# | 
# | -----------------------------------------------------------------------------------------------------------------------
for( i in 1:length(prob))
{

  while (niter < niter_max)

  {

    Fx=function(x) # Equation (7) # ! Evaluate function at old estimate
    {

      require(PearsonDS)
      return(ppearsonIII(x,shape=3.743843 ,location= 0,scale= 7.976308)*
               ppearsonIII(x,shape=1.492779  ,location=3.295901, scale=9.212522)-prob[i]);
    }

    dFx=function(x) # Equation (8) # ! Evaluate derivative at old estimate
    {
      require(PearsonDS) 
      return((dpearsonIII(x,shape=3.743843 ,location= 0,scale= 7.976308))*
               (ppearsonIII(x,shape=1.492779  ,location=3.295901, scale=9.212522)) +
               (dpearsonIII(x,shape=1.492779  ,location=3.295901, scale=9.212522))*
               ppearsonIII(x,shape=3.743843 ,location= 0,scale= 7.976308));
    }
# | -------------------------------------------------------------------------------------------------------------------------------------
# | A function of the Newton-Raphson algorithm to calculate quantiles of product model
# | Description : Applies the Newton-Raphson algorithm to find x such that Qx+1 = X[k]- F(x)/F'(x) == 0.
# | Returns the value of x at which Qx+1 = X[k]- F(x)/F'(x) == 0.
# | --------------------------------------------------------------------------------------------------------------------------------
    Newton.Raphson <-function(Fx,dFx,x) # Equation (6)
    {

      if (abs(dFx(x))<10*.Machine$double.eps)
      {
        return (x);
      } else
      {
        return(x-Fx(x)/dFx(x)); # ! Calculate new estimate
      }
    }
    n=n+1
    x.new<-c(x.new,Newton.Raphson(Fx,dFx,x.new[n-1]));
    abline(a=Fx(x.new[n])-dFx(x.new[n])*x.new[n],b=dFx(x.new[n]),col=col[n-1]);
    if(abs(x.new[n]-x.new[n-1])<100*.Machine$double.eps) break;
    niter = niter+1 ;
    Sys.sleep(1)
  }

  x.new[i]<-cbind(x.new[i]);
  cat(paste())
  print(paste("at each doing step i--------------------------------------------------------- >",prob[i],x.new))

}  
# | --------------------------------------------------------------------------------------------------------------------------------------
# | End of quantile calculation here
# | ------------------------------------------------------------------------------------------------------------------------------------

我希望在 for 循环迭代结束后具有与此表相同的值。

prob        Quantiles
 [1,] 0.500  29.88999
 [2,] 0.650  35.62553
 [3,] 0.750  40.46182
 [4,] 0.800  43.47660
 [5,] 0.850  47.20123
 [6,] 0.900  52.21923
 [7,] 0.950  60.35834
 [8,] 0.998  94.53630
 [9,] 0.999 101.46150

【问题讨论】:

    标签: loops for-loop


    【解决方案1】:

    我不完全确定您在问什么,但您似乎只需要在您的 for 循环中添加一个 if 语句。这是一些如何做到这一点的代码:

    在 Python 中:

    for i in range(0,10):
        if i == 9:
            x=(the value you need)    #101.46150
    

    在 C++ 中:

    for( int i = 0; i < 10; i++ )
    {
        if i==9
        {
            x=(the value you need);   #101.46150
        }
    }
    

    在 R 中:

    for (i in 0:9 ) {
       if(i==9) {
          x=(the value you need);   #101.46150
       }
    }
    

    【讨论】:

    • 谢谢,但我正在使用 r
    • @Firaol 您应该更新您的问题以包含该信息
    • 再次感谢,上面的脚本运行良好,如果您在 R 中运行,您必须通过单击图表来选择起始值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多