有人知道如何在for循环中提取最后的interation值吗?例如,我有一个连续的
迭代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
# | ------------------------------------------------------------------------------------------------------------------------------------
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
答案 0 :(得分:0)
我不完全确定你在问什么,但似乎所有你需要的是if
循环中的for
语句。以下是一些如何执行此操作的代码:
在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
}
}