How I plotted in this function was to create an output sequence,
x <- seq(-5, 5, by = 0.01)
then I initialized two y vectors
y100<-c()
y1000 <-c()
then I just wrote a for loop that is going through all the values of x
for(i in 1:length(x)){
y100[i]<- binom(parameter = x[i], trials = 10, success.vector = draw100)
y1000[i]<- binom(parameter = x[i], trials = 10, success.vector = draw1000)
}
I'm not sure it's the best method, but it works to get all the values you
need.
Cheers,
Zac
On Tue, Mar 3, 2009 at 7:55 PM, Christopher Michael Muller <
muller at fas.harvard.edu> wrote:
Hi everyone,
I have a very basic question about plotting in 1f.
In the past, to plot a function I've done the following:
# Plot a divide function
divide <- function(x, y){
out <- x/y
return(out)
}
x <- seq(.01, .99, .01)
y <- divide(x = seq(.01, .99, .01), y = 2)
plot(x, y, type="l")
This doesn't work for my Log-Likelihood function because
ll.binomial(par = seq(.01, .99, .01), y = y, N = 10)
[1] -595.2
returns a single number and this error message:
Warning messages:
1: In y * log(binompi) :
longer object length is not a multiple of shorter object length
2: In (N - y) * log(1 - binompi) :
longer object length is not a multiple of shorter object length
Any thoughts about how to remedy this? Are there alternative ways to plot?
Thanks,
chris
---------- Forwarded message ----------
From: Lai, Ronald <rolai at hbs.edu>
Date: Mon, Mar 2, 2009 at 9:08 AM
Subject: Re: [gov2001-l] PS4#2 & 1f
To: "gov2001-l at lists.fas.harvard.edu" <gov2001-l at
lists.fas.harvard.edu>
Thanks all. I get it!
Best.
*From:* gov2001-l-bounces at
lists.fas.harvard.edu [mailto:
gov2001-l-bounces at
lists.fas.harvard.edu] *On Behalf Of *Patrick Lam
*Sent:* Monday, March 02, 2009 3:11 AM
*To:* gov2001-l at
lists.fas.harvard.edu
*Subject:* Re: [gov2001-l] PS4#2 & 1f
whoops, John-Paul is also right, the y-axis should be on the same scale.
On Mon, Mar 2, 2009 at 3:06 AM, Patrick Lam <plam at fas.harvard.edu>
wrote:
It is actually the x-axis that needs to be scaled the same. Ronald,
you're
right that technically you can do the same by
plotting the two curves on
two
different graphs and then eyeballing them, as
long as the x-axis is the
same. However, since we can shift likelihoods up and down, it's easier
to
compare by putting them on the same graph, which
is what the problem is
asking.
On Sun, Mar 1, 2009 at 11:05 PM, John-Paul Ferguson <jpferg at mit.edu>
wrote:
2) Vertical shifts? Is it not the same as using
>par(mfrow=c(2,1)) and
eyeballing the results?
If you plot the two using par(mfrow=c(2,1)), the curvature will look
almost
the same, but the scale of the y-axis will be
quite different. You have
to
plot them on the same scale in order to compare
the curvature. Shifting
one
of them vertically so that their maxima have the
same y-value makes it
easy
to plot them on the same graph.
--John-Paul
On Sun, Mar 1, 2009 at 9:53 PM, Lai, Ronald <rolai at hbs.edu> wrote:
All,
Since both distributions are Binomials with N=10, the log likelihood f(x)
would be the same for both. Why is it necessary to even have an
indicator
variable? Essentially f(x)^d * f(x)^(1-d) =
f(x).. I guess if one distr
was
Bin w/ N=10 and the other was N<>10, having
the indicator variable makes
sense. I'll operate on the assumption that the user of my R code can
alter
Ns for the two distributions.
I'm a bit confused about PS4#1f
1) I'm assuming reparameterization is not necessary for plotting so I
rewrote the f(x) to deal w/ this
2) Vertical shifts? Is it not the same as using par(mfrow=c(2,1)) and
eyeballing the results?
Feel like I'm missing something.
Best.
PS.
Sparsha, in your f(x) below, you have not defined n or g. to my
knowledge,
the function also does not require an iterative
process (per your
for-loop)
*From:* gov2001-l-bounces at
lists.fas.harvard.edu [mailto:
gov2001-l-bounces at
lists.fas.harvard.edu] *On Behalf Of *sparsha saha
*Sent:* Friday, February 27, 2009 3:41 PM
*To:* gov2001-l at
lists.fas.harvard.edu
*Subject:* [gov2001-l] R hates me
is anyone getting this error message for problem 2 on this week's problem
set
Error in pnorm(pa3) : element 1 is empty;
the part of the args list of '.Internal' being evaluated was:
(q, mean, sd, lower.tail, log.p)
I get this when I run my log likelihood function in R and then try to use
optim on it:
binomial.second <- function(pa2, pa3, y2, n2)
{
+ pies2 <- pnorm(pa2)
+ pies3 <- pnorm(pa3)
+ for (i in 1:n) {
+ blaba <- ifelse(g > 0, sum(y2 * log(pies2) + (n - y2) * log(1 -
pies2)),
sum(y2 * log(pies3) + (n - y2) * log(1 -
pies3)))
+ }
+ roe <- sum(blaba) / n
+ return(roe)
+ }
>
> opt123 <- optim(par = 0.25, fn = binomial.second, method = "BFGS",
control
= list(fnscale = -1), y = data, n = 10)
Error in pnorm(pa3) : element 1 is empty;
the part of the args list of '.Internal' being evaluated was:
(q, mean, sd, lower.tail, log.p)
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Patrick Lam
Department of Government and Institute for Quantitative Social Science,
Harvard University
http://www.people.fas.harvard.edu/~plam<
http://www.people.fas.harvard.edu/%7Eplam>
--
Patrick Lam
Department of Government and Institute for Quantitative Social Science,
Harvard University
http://www.people.fas.harvard.edu/~plam<
http://www.people.fas.harvard.edu/%7Eplam>
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l