I want to simulate 100 observations from a beta distribution using the Newton Raphson Method, where the stopping rule is |xi − xi−1| < .05 and the starting point for the algorithm is 0.5 and want to find out on average how many iterations are required until the observation is accepted
R code to simulate a sample with shape1 = 3
and shape2 = 5
:
x <- rbeta(n=100, tol=1e-7, shape1=3, shape2=5)
foo<-function(x,tol=1e-7, x0=0.5,N=100)
{
x1=x0
h=1e-7
i=0.5;
p<-numeric(N)
while(i<=N)
{ df.dx=(x(x0+h)-x(x0))/h
x1=(x0-(x(x0)/df.dx))
p[i]=x1
i=i+1
if(abs(x1 - x0) < tol) break
x0=x1}
return (p[1:(i-1)])
}
When I try to call function it says function not found.