'Solving Intermediate Microeconomics Problems in R
Right now im trying to figure out how to solve standard constrained optimization problems in R which would usually be solved by hand with the use of a lagrangian.
The code I have so far is:#Our Legrangian
L<-expression(X^a*Y^b+l*(M-px*X-py*Y))
#First Order Conditions:
dLdX<-D(L,"X")
dLdY<-D(L,"Y")
dLdl<-D(L,"l")
I am unable to work through the rest of this. I've tried defining the first order conditions as a matrix and some vector b=c(0,0,0)
and use solve()
however since this is a non-linear problem with symbols its problematic.
Is there a solution to this problem?
Solution 1:[1]
Two years later I have learned that the easiest way to solve these types of problems is with the NlcOptim
package. The code used for a sample procedure is the following:
############################
#Utility Maximization in R#
###########################
library('NlcOptim')
library('MASS')
##############
#Preferences#
#############
preferences<-function(x){
return(-x[1]^0.5*x[2]^0.5)
}
###################
#Budget Constraint#
###################
budgetconstraint<-function(x){
f=NULL
f= rbind(f,2*x[1]+1*x[2]-10)
return(list(ceq = NULL, c = f))
}
######################
# Starting values ###
####################
x0<-c(1,1)
########
#Solve#
#######
solnl(x0,consumer,budgetconstraint)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | EconJohn |