-2

I'm running models with various initial values, and I'm trying to append values (3 estimators) by rows to a dataframe in a loop. I assign values to estimators within the loop, but I can't recall them to produce a dataframe.

My code: f is the model for the estimation. Three parameters: alpha, rho, and lambda in the model. I want to output these 3 values.

library("maxLik")

f <- function(param) {
    alpha <- param[1]
    rho <- param[2]
    lambda <- param[3]
    u <- 0.5 * (dataset$v_50_1)^alpha - 0.5 * lambda * (dataset$v_50_2)^alpha
    p <- 1/(1 + exp(-rho * u))
    logl <- sum(dataset$gamble * log(p) + (1 - dataset$gamble) * log(1 - p))
}


df <- data.frame(alpha = numeric(), rho = numeric(), lambda = numeric())

for (j in 1:20) {
    tryCatch({
        ml <- maxLik(f, start = c(alpha = runif(1, 0, 2), rho = runif(1, 0, 4), lambda = runif(1, 
            0, 10)), method = "NM")

        alpha[j] <- ml$estimate[1]
        rho[j] <- ml$estimate[2]
        lambda[j] <- ml$estimate[3]

    }, error = function(e) {NA})
}
output <- data.frame(alpha, rho, lambda)

error occurs:

 Error in data.frame(alpha, rho, lambda) : object 'alpha' not found

Expected output

 alpha    rho    lambda
  0.4      1       2      # estimators append by row. 
  0.6      1.1     3      # each row has estimators that are estimated 
  0.7      1.5     4      # by one set of initial values, there are 20 
                          # rows, as the estimation loops for 20 times.
Lumos
  • 1,303
  • 2
  • 17
  • 32
  • Please mention the library maxLik belongs to – Hardik Gupta Sep 26 '17 at 09:15
  • 1. do it all in a list, then do.call(rbind,mylist), 2. i would rather make a new column with the RUN-INDEX, than a own row. 3. in the error function i would record the run that failed also. You can get rid of it anyways later. – Andre Elrico Sep 26 '17 at 09:15
  • also please mention what is f? – Hardik Gupta Sep 26 '17 at 09:18
  • @AndreElrico Do you mean appending all the values in the list, rather than a dataframe? So do not use dataframe at the very first? – Lumos Sep 26 '17 at 09:25
  • u made a change to your post. not really sure what you mean anymore. Try to make an example that is easy and still contains your problem. – Andre Elrico Sep 26 '17 at 09:32
  • I am unable to reproduce your exmaple as you have not mentioned about `f` – Hardik Gupta Sep 26 '17 at 09:39
  • @Hardikgupta thanks for your patience, I have edited the question with f. – Lumos Sep 26 '17 at 09:41
  • function f accepts parameter? please check this, it also has `dataset` which is missing – Hardik Gupta Sep 26 '17 at 09:42
  • @Hardikgupta What do you mean exactly?`f` has 3 parameters that need to be estimated in `maxLik`, that's why they are listed in `f`. – Lumos Sep 26 '17 at 09:45
  • it gives me this error when I run your code `Error in fnOrig(theta, ...) : object 'dataset' not found` – Hardik Gupta Sep 26 '17 at 09:46
  • @Hardikgupta `dataset` is the dataframe of my raw data, which I did not put here, and `dataset$v_50_1` and `dataset$v_50_2` are the columns in the dataframe. – Lumos Sep 26 '17 at 09:49
  • If you change `error = function(e)` from `NA` to `message(e)` it outputs `object 'dataset' not found`. Please post a fake `dataset` for us to be able to see what's wrong. Use the RNG's that come with `R` to produce it. – Rui Barradas Sep 26 '17 at 10:02

1 Answers1

1

I am running an example, by changing the function f

library("maxLik")

t <- rexp(100, 2)
loglik <- function(theta) log(theta) - theta*t


df <- data.frame(alpha = numeric(), rho = numeric(), lambda = numeric())

for (j in 1:20){
  tryCatch({
    ml <- maxLik(loglik, start = c(alpha = runif(1, 0, 2), rho = runif(1, 0, 4), 
                              lambda = runif(1, 0, 10)), method = "NM")

    df <- rbind(df, data.frame(alpha = ml$estimate[1], 
                               rho = ml$estimate[2], 
                               lambda = ml$estimate[3]))
    # I tried to append values for each column         
  }, error = function(e) {NA})}

> row.names(df) <- NULL
> head(df)
     alpha      rho   lambda
1 2.368739 2.322220 2.007375
2 2.367607 2.322328 2.007093
3 2.368324 2.322105 2.007597
4 2.368515 2.322072 2.007334
5 2.368269 2.322071 2.007142
6 2.367998 2.322438 2.007391
Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83