【发布时间】:2015-03-23 20:56:31
【问题描述】:
有人知道如何在 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
# | ------------------------------------------------------------------------------------------------------------------------------------
【问题讨论】:
标签: r