Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何在R中获取for循环迭代的最后一个值?_Loops_For Loop - Fatal编程技术网

Loops 如何在R中获取for循环迭代的最后一个值?

Loops 如何在R中获取for循环迭代的最后一个值?,loops,for-loop,Loops,For Loop,有人知道如何提取for循环内部的最后一个交互值吗?例如,我有一个连续的迭代 94.53630、94.53630、94.53630、99.57083、101.29593、101.46015、101.46150、101.46150、101.46150、101.46150、101.46150。这是在每个执行步骤,但我只想提取最后一个(101.46150)。因为,我运行了9次for循环,并且我正在查看迭代9值的末尾。在这个R脚本中,我希望在第一次迭代之后,只取一个x.new和相应的概率值 # | ---

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

94.53630、94.53630、94.53630、99.57083、101.29593、101.46015、101.46150、101.46150、101.46150、101.46150、101.46150。这是在每个执行步骤,但我只想提取最后一个(101.46150)。因为,我运行了9次for循环,并且我正在查看迭代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
循环中使用
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@Firaol您应该更新您的问题以包含该信息。再次感谢,上面的脚本运行良好,如果您在R中运行,您必须通过单击图形来选择开始值。
for( int i = 0; i < 10; i++ )
{
    if i==9
    {
        x=(the value you need);   #101.46150
    }
}
for (i in 0:9 ) {
   if(i==9) {
      x=(the value you need);   #101.46150
   }
}