I'm trying to solve a non-linear equation with nleqslv, and I know a priori if I want a positive or negative solution (it's a dataset on choice under risk, and I'm trying to compute the risk aversion coefficient for each individual under CRRA assumption. Since I can observe the DMs' choices, I already know if each DM is risk averse or not). Is there any way to enforce it?
I know I could try with different initial values; however, I'd like to find another way as I'm using nleqslv in a for loop (I must compute one solution for each observation) and I can't find an initial guess that works for everyone.
My code is the following:
`bernoulli <- function(x, r) {
ifelse(x>=0,((x+1)^(1-r)-1)/(1-r), -((-x+1)^(1-r)-1)/(1-r) )
}
bernoulli.log <- function(x) {
ifelse(x>=0, log(x+1), -log(-x+1) )}
mydata$alpha_crra <- NA
for (i in 1:nrow(mydata)) {
indiff.eq.crra <- function(r) {
return(bernoulli(mydata$CE[i], r) - mydata$p[i]*bernoulli(mydata$win[i], r)
- (1-mydata$p[i])*bernoulli(mydata$lose[i], r))
}
mydata$alpha_crra[i] <- ifelse(mydata$riskneutral[i] == 1, 0,
ifelse(abs(bernoulli.log(mydata$CE[i]) -
mydata$p[i]*bernoulli.log(mydata$win[i]) -
(1-mydata$p[i])*bernoulli.log(mydata$lose[i])) < 0.001 &
mydata$riskaverse[i] == 1, 1,
nleqslv(-5, indiff.eq.crra)$x))
}`
where:
mydata$win[i]= the high payoff of the lottery (can change depending on the observation) mydata$lose[i]= the low payoff of the lottery mydata$p[i] = probability to win the high payoff mydata$CE[i]= the Certainty Equivalent stated by DM i mydata$riskneutral[i] = a dummy variable = 1 if i is risk neutral, 0 otherwise mydata$riskaverse[i] = a dummy variable = 1 if i is risk averse, 0 otherwise