R optimization and curve()?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

R optimization and curve()?

jcress410
I'm trying to figure out how to plot basic utility maximization results with R, Ideally I'd like to plot the value of u through x1,x2 space, so you can graph income / substitution effects easily...

also, it'd be nice if I could put a linear budget constraint on the graph

 here's an example with cobb douglas utility

u <- function(x) {
    x1 <- x[1]
    x2 <- x[2]
 (x1^alpha)*(x2^(1-alpha))
}
utility <- function(x) x[1]^(alpha)*x[2]^(1-alpha)
p <- c(2,1)
i <- -100
alpha <- .3
umax <- function(p,i,u) {
res <- constrOptim(c(.5,.5), u, grad=NULL, ui=-p, ci=i, mu = 1e-04, control=list(fnscale=-1))
return(res)
}
curve(umax, c(c(2,1),c(2,1)), c(10,100))
Reply | Threaded
Open this post in threaded view
|

Re: R optimization and curve()?

Ray Brownrigg-2
On Wed, 13 Oct 2010, jcress410 wrote:

> u <- function(x) {
>     x1 <- x[1]
>     x2 <- x[2]
>  (x1^alpha)*(x2^(1-alpha))
> }
> utility <- function(x) x[1]^(alpha)*x[2]^(1-alpha)
> p <- c(2,1)
> i <- -100
> alpha <- .3
> umax <- function(p,i,u) {
> res <- constrOptim(c(.5,.5), u, grad=NULL, ui=-p, ci=i, mu = 1e-04,
> control=list(fnscale=-1))
> return(res)
> }
> curve(umax, c(c(2,1),c(2,1)), c(10,100))

I don't see any question here.

However you must RTFM.

1) The returned value of umax() is a list.  How do you expect curve() to plot a list?

2) curve() requires that the function umax() must be vectorisable wrt its first parameter,
i.e. you pass it a numeric vector and it returns a numeric vector of the same length.

3) you have to realise that curve() will only graph a 1-dimensional function

HTH,
Ray

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: R optimization and curve()?

jcress410
Ray Brownrigg-2 wrote
I don't see any question here.

However you must RTFM.
You're awful at the internet. People get intimidated about posting questions to lists because of threads like this.  Most of the time when you want to say RTFM you should say (at least) say which manual to read.

Ray Brownrigg-2 wrote
1) The returned value of umax() is a list.  How do you expect curve() to plot a list?

2) curve() requires that the function umax() must be vectorisable wrt its first parameter,
i.e. you pass it a numeric vector and it returns a numeric vector of the same length.

3) you have to realise that curve() will only graph a 1-dimensional function

HTH,
Ray
but, there has to be some way to represent a multivariate function in terms of two of its arguments, i just dont know what to use.

Reply | Threaded
Open this post in threaded view
|

Re: R optimization and curve()?

Arun.stat
See ?persp

Also there are some really good examples on 3D plots in "rgl" package.

Best,