-1

H, i am a beginner in R and I have problems dealing with for loops. What I am trying to do is store 25 values of tau, b0 and b1. In order to find b1 we have to find the lowest value of Out function in the code ( b1 should be close to the value 1 ) and I want use this b1 in the for loop to find b0 and b1 and should be stored in a vector of length 25 ( I should have a list of b0 ,b1 tau). Please help and thanks in advance! I have added what I intend to do

I want

 b1[1], b1[2],..., b1[25]

t[1],t[2],...,t[25]

b0[1],b0[2],...,b0[25] 

by solving the problem in the picture

enter image description here

set.seed(22)
#Inverse Transformation on CDF
Simburr.f1 <- function(n, tau) {
  u=runif(n)
  x<- runif(n)
  lambda = exp(1+x)
  y= (1/(u^(1/lambda))-1)^(1/tau)
  y
}
y33 = Simburr.f1(25,0.5)
#------------------------------------
# We fix Tau = 0.5
est3 = function(m) {
  x=runif(25)
  Out = rep(0,m)
  k= seq(0.05,2,l=m)
   for ( i in 1:m){
    Out[i]=  log(mean(x)) -log(mean(exp(k[i]*x)*log(1+y33^0.5)*x) )    +  log(mean(log(1+y33^0.5)*exp(k[i]*x)))  
     }
  Out
}
p=est3(200)
k[which.min(abs(p-0))] # = B1
#--------------------------------------
x =runif(25)
tau[1]= 0.5  
b0[1] = 1
b1[1] = 1
# Iterative process
for ( i in 2:25){
  b0[i]= -log(mean(log(1+y33^(tau[i-1]))*exp(b1[i-1]*x)))
  tau[i] =1/( mean((exp(b0[i-1]+b1[i-1]*x)+1)*(y33^tau[i-1])*log(y33)/(1+y33^tau[i-1])) -mean(log(y33)))
}
Murad
  • 15
  • 5
  • Since you know the length of the vectors, why not declare them before the `for`-loop; `b0 <- b1 <- tau <- vector(mode = "numeric", length = 25L)` – tstev Aug 06 '18 at 08:33

3 Answers3

0

In terms of your original question, you're nearly there. But you can't declare variables by specifically defining their first element with [1]. Rather you need to either declare an empty array then write to it:

b0 = NULL
b0[1] = 1

Or you can simply declare b0 by writing 1 to it:

b0 = 1

In both cases, this will produce a vector of length 1. The code you're using will extend the length of the vector as it runs.

Note: your code still won't run, as you've referenced the variable k which only exists in the scope of a function, you'll need to correct that too. But I'm not sure what you're wanting to acheive with k[which.min(abs(p-0))] # = B1 so I can't help with that bit.

Mike S
  • 312
  • 1
  • 8
  • I have defined b0[1] = 1 in the latter stage. – Murad Aug 06 '18 at 08:37
  • k is a list of b1 values between 0.05 and 2 of length m and in that bit I want to find b1[1]. b1[1] value is the smallest value of out function ( closest to zero in magnitude) and this is what k[which.min(abs(p-0))] is doing. – Murad Aug 06 '18 at 08:42
  • The way you have tried to declare b0, using b0[1] = 1, is not valid in R. So it fails and your for loop doesn't have a b0 (or b1 or tau) to write to. Use the method I mentioned, or one of the methods the other answers mention. Overall I think you have a few other errors in your code, for example your statement using k will simply write an output to the console, it won't do anything with it. I think you want that be a return value of the function but I'm not clear. If you provide a description of the full algorihm then people will be able to help better. – Mike S Aug 06 '18 at 09:01
0

I don't quite get what the question is. But, I clearly understand that your code isn't working because you can't put the value of tau[1], b0[1], and b1[1] before explicitly declare the variable as list or matrix. You need to add this code before you put the value into the first index:

tau <- matrix(c(0),ncol=1,nrow=25)
b0 <- matrix(c(0),ncol=1,nrow=25)
b1 <- matrix(c(0),ncol=1,nrow=25)
tau[1]= 0.5  
b0[1] = 1
b1[1] = 1

and here might be what you're looking for:

for ( i in 2:25){
  p <- est3(200)
  b1[i] = p[which(abs(p-0)==min(abs(p-0)))]
  b0[i]= -log(mean(log(1+y33^(tau[i-1]))*exp(b1[i-1]*x)))
  tau[i] =1/( mean((exp(b0[i-1]+b1[i-1]*x)+1)*(y33^tau[i-1])*log(y33)/(1+y33^tau[i-1])) -mean(log(y33)))
}

b1 values are from the value of est3(200) that closest to zero

The complete code:

set.seed(22)
#Inverse Transformation on CDF
Simburr.f1 <- function(n, tau) {
  u=runif(n)
  x<- runif(n)
  lambda = exp(1+x)
  y= (1/(u^(1/lambda))-1)^(1/tau)
  y
}
y33 = Simburr.f1(25,0.5)
#------------------------------------
# We fix Tau = 0.5
est3 = function(m) {
  x=runif(25)
  Out = rep(0,m)
  k= seq(0.05,2,l=m)
   for ( i in 1:m){
    Out[i]=  log(mean(x)) -log(mean(exp(k[i]*x)*log(1+y33^0.5)*x) )    +  log(mean(log(1+y33^0.5)*exp(k[i]*x)))  
     }
  Out
}


#--------------------------------------
x =runif(25)
tau <- matrix(c(0),ncol=1,nrow=25)
b0 <- matrix(c(0),ncol=1,nrow=25)
b1 <- matrix(c(0),ncol=1,nrow=25)
tau[1]= 0.5  
b0[1] = 1
b1[1] = 1


# Iterative process
for ( i in 2:25){
  p=est3(200)
  b1[i] = p[which(abs(p-0)==min(abs(p-0)))]
  b0[i]= -log(mean(log(1+y33^(tau[i-1]))*exp(b1[i-1]*x)))
  tau[i] =1/abs(( mean((exp(b0[i-1]+b1[i-1]*x)+1)*(y33^tau[i-1])*log(y33)/(1+y33^tau[i-1])) -mean(log(y33))))
}
  • Out is a difference between two functions that should be equal and the b1 value is the one that is closest to zero. – Murad Aug 06 '18 at 08:50
  • I will provide hand written of what im trying to achieve – Murad Aug 06 '18 at 08:51
  • if you want dynamic values of b1, you need to specify the 'p' inside the loop. and then pick the value from p that closest to 0. I have edited my answer you can check it and tell me if it was what you wanted to achieve – Rendy Eza Putra Aug 06 '18 at 09:09
  • I am trying to put your code in and get error Error : in b1[i] <- p[which(abs(p - 0) == min(abs(p - 0)))] : replacement has length zero – Murad Aug 06 '18 at 09:20
  • your codes run but values of tau , b0 and b1 should be close to their initial values and tau is postive – Murad Aug 06 '18 at 09:29
  • 1
    I don't know the formulas of those. You can specify the formulas by yourself. At least now your code is working. The mistake that you did was you didn't declare the variables as vector or matrix before you specified the first index – Rendy Eza Putra Aug 06 '18 at 09:33
  • you can add abs() into the tau values to get positive tau – Rendy Eza Putra Aug 06 '18 at 09:38
  • have a look at what i uploaded – Murad Aug 06 '18 at 10:00
  • @Murad - not sure this is the place for the mathematics behind your script. There have been enough suggestions to get your script to work. I suggest you try one. – tstev Aug 06 '18 at 11:09
0

Before you declare your initial tau, b0 and b1 values you need to declare the variables by adding the following line; tau <- b0 <- b1 <- vector("numeric", 25L).

EDIT

set.seed(22)
#Inverse Transformation on CDF
Simburr.f1 <- function(n, tau) {
  u=runif(n)
  x<- runif(n)
  lambda = exp(1+x)
  y= (1/(u^(1/lambda))-1)^(1/tau)
  y
}
y33 = Simburr.f1(25,0.5)
#------------------------------------
# We fix Tau = 0.5
est3 = function(m) {
  x=runif(25)
  Out = rep(0,m)
  k= seq(0.05,2,l=m)
  for ( i in 1:m){
    Out[i]=  log(mean(x)) -log(mean(exp(k[i]*x)*log(1+y33^0.5)*x) )    +  log(mean(log(1+y33^0.5)*exp(k[i]*x)))  
  }
  Out
}
p=est3(200)
k <- seq(0.05,2, length.out = 200)
k[which.min(abs(p-0))] # = B1
#--------------------------------------
x =runif(25)
tau <- b0 <- b1 <- vector("numeric", 25)
tau[1]= 0.5  
b0[1] = 1
b1[1] = 1
# Iterative process
for ( i in 2:25){
  b0[i]= -log(mean(log(1+y33^(tau[i-1]))*exp(b1[i-1]*x)))
  tau[i] =1/( mean((exp(b0[i-1]+b1[i-1]*x)+1)*(y33^tau[i-1])*log(y33)/(1+y33^tau[i-1])) -mean(log(y33)))
}
tstev
  • 607
  • 1
  • 10
  • 20
  • I dont understand what you mean. – Murad Aug 06 '18 at 08:47
  • Your script is trying to write a value to `tau[1]` but `tau` hasn't been defined previously in your script. So define `tau` first as a vector with length 25 and then you can run your for loop and store values in `tau`. – tstev Aug 06 '18 at 08:49