To cut a long story short: I would like to estimate the parameter r of a utility function, which depends on demographic variables. I wrote a code for the utility function - that should be maximised -, transformed it into a log lik function and use it in mle2. I get a parameter estimate for r, but I do not get the parameters for the demographic variables.
Now the long story:
In specific, I want to estimate the parameter r in the following function:
CRRA <- function(x, r){
u <- (x ^ (1-r)) / (1-r)
return(u)
}
r describes risk attitude and I want to maximise utility u. r in turn depends on personal characteristics such that
r <- beta0 + beta1 * covar[1] + beta2 * covar[2] + beta3 * covar[3]
where the covars are known and I want to estimate beta0, beta1, ... , betaN and r.
I have choice data from different lottery games (multiple price lists) with 10 decision situations each. In each decision situation, the player needs to decide whether he/she wants to play lottery A or lottery B (choice data). A1, A2, B1 and B2 are the possible payoffs x and prob is the respective probability to receive the associated payoff. In lottery A the expected utility of a specific decision situation is
EU <- function(p, x, r){ #expected utility of one of the two lotteries in a pair
eu <- p * CRRA(x[[1]], r) + (1-p) * CRRA(x[[2]], r)
return(eu)
}
EU(p = d[,("prob"), x = d[,("A1", "A2", "B1", "B2")], r)
The expected utility of the entire decision situation is
DELTA <- function(x, p, r){ #difference of lottery B (right, more risky) and lottery A (left, safe)
delta <- EU(p, x[c(1,2)], r) - EU(p, x[c(3,4)], r)
return(delta)
}
and depends on the choice variable. The choice variable is = 0 when lottery A is chosen and = 1 when lottery B is chosen.
Accordingly, the log likelihood function looks as follows:
Lc2 <- function(r){
dl <- DELTA(x, prob, r)
lc <- -sum(choice * pnorm(dl[1], log.p = TRUE)
(1 - choice) * pnorm(1 - dl[1], log.p = TRUE))
return(lc)
}
Using Lc2 in mle2 provides the following results:
> fit2 <- mle2(Lc2,
+ data = df,
+ start = c(r=0.76),
+ parameters = list(r ~ age+ha+microbes))
> tidy(fit2)
# A tibble: 1 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 r 0.512 0.0445 11.5 1.21e-30
> summary(fit2)
Maximum likelihood estimation
Call:
mle2(minuslogl = Lc2, start = c(r = 0.76), data = df, parameters = list(r ~
covar[1] + covar[2] + covar[3]))
Coefficients:
Estimate Std. Error z value Pr(z)
r 0.512122 0.044503 11.508 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-2 log L: 743.4722
How do I get mle2 to calculate also parameter estimates for beta? Why does parameter = list(r ~ covar[1] + covar[2] + covar[3])
inside mle2
not provide me the beta estimates for each covar? Where do I need to place the r <- ...
function?
Thanks a lot in advance for your help on this.
I tried the following:
U <- function(x, prob, params, covar){
A1 <- x[1]
A2 <- x[2]
B1 <- x[3]
B2 <- x[4]
prob <- prob
r <- params[1]
beta0 <- params[2]
beta1 <- params[3]
beta2 <- params[4]
beta3 <- params[5]
r <- beta0 + beta1 * covar[1] + beta2 * covar[2] + beta3 * covar[3]
u <- DELTA(x = c(A1, A2, B1, B2), prob = prob, r)
return(u)
}
loli <- function(params){
d <- datan
x <- d[,c("A1","A2","B1","B2")]
prob <- d[,"prob"]
covar <- d[,c("cov1", "cov2", "cov3")] %>% as.matrix(.)
dl <- U(x = x, prob = prob, params = params, covar = covar)
choice <- d[,"choice"]
lc <- -sum(choice * pnorm(dl[[1]], log.p = TRUE) +
(1 - choice) * pnorm(1 - dl[[1]], log.p = TRUE))
return(lc)
}
guess <- list(params = c(0.76, 1, 1, 1, 1))
... without success:
> fit3 <- mle2(loli,
+ start = guess,
+ data = choices_DL)
Error in validObject(.Object) :
invalid class “mle2” object: invalid object for slot "fullcoef" in class "mle2": got class "NULL", should be or extend class "numeric"