Wow! That was SUPER helpful! Thanks!
Sincerely,
Olena Ageyeva
From: clee at
hbs.edu
To: gov2001-l at
lists.fas.harvard.edu
Date: Sun, 22 Feb 2009 00:00:48 -0500
Subject: Re: [gov2001-l] PS3 3.2
Olena,
What you want to do is to use the expand.grid() function.
This is how it works:
Suppose you do this:
olena_grid <- expand.grid(x=c(1,2,3), y=c(1,2,3))
This will give you
x y
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
Basically, a 3x3 data grid of (x,y values). Then, if you
create a function that takes the olena_grid as input and output a bunch of z
values, you can then use the z values, along with the olena_grid as the input
to the wireframe. Let?s do that by combinging olena_grid with the new
z values into another data frame that looks like this, called olena_grid2:
x y z
1 1 2
2 1 3
3 1 4
1 2 3
2 2 4
3 2 5
1 3 4
2 3 5
3 3 6
Then, try to call wireframe with the data.frame olena_grid2 this
way:
wireframe(z~x*y, data=olena_grid2)
The only part I?m not sure about is how the z~x*y behaves,
but for the purpose of this assignment, once you input the function from the
problem, it didn?t matter for me. Maybe Patrick can help us explain
how z ~ x*y works.
Hope this helps,
Clarence
From: gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of Olena
Ageyeva
Sent: Saturday, February 21, 2009 11:49 PM
To: gov2001-l at
lists.fas.harvard.edu
Subject: Re: [gov2001-l] PS3 3.2
Thanks! It worked.
Another question. How to initialize intervals for x and y in wireframe()?
Sincerely,
Olena Ageyeva
From: clee at
hbs.edu
To: gov2001-l at
lists.fas.harvard.edu
Date: Sat, 21 Feb 2009 23:40:02 -0500
Subject: Re: [gov2001-l] PS3 3.2
Hi
Olena,
The
problem with your code is that you?ve specified your function to input
two parameters. I believe optim() only allows an input of one parameter
for its functions. Hence, the way to use optim() for two dimension
functions is to declare your function with one input as a vector of x and y.
So in your case, try declaring your x_y_density like this:
x_y_density<-function(input){
x<-input[1]
y<-input[2]
exp(-((x-2)^2+(y-1)^2))
}
Note
one of the examples from the help file does things exactly this way:
fr
<- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
optim(c(-1.2,1),
fr)
Hope
this helps,
Clarence
From:
gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of Olena
Ageyeva
Sent: Saturday, February 21, 2009 11:02 PM
To: gov2001-l at
lists.fas.harvard.edu
Subject: [gov2001-l] PS3 3.2
Hi
all,
Can anyone explain to me how to use optim() for two dimension function?
Here is my code:
x_y_density<-function(x,y){exp(-((x-2)^2+(y-1)^2))}
...
par=c(0,0.05)
optim(par, x_y_density, method="BFGS")
I get the following error message and don't know how to fix it:
par=c(0,0.05)
optim(par, x_y_density(par), method="BFGS")
Error in x_y_density(par) : element 1 is empty;
the part of the args list of '-' being evaluated was:
(y, 1)
Thanks!
Sincerely,
Olena Ageyeva
Windows
Live?: Discover 10 secrets about the new Windows Live. View
post.
Windows
Live? Hotmail??more than just e-mail. See how it works.
_________________________________________________________________
Windows Live?: Discover 10 secrets about the new Windows Live.
http://windowslive.com/connect/post/jamiethomson.spaces.live.com-Blog-cns!5…