I'm not going to download your data since it's probably irrelevant to your
problem and I have no interest in downloading unknown files.

Consider this instead:

library(quantmod)
SPY = getSymbols("SPY",auto.assign=F)
SPY = weeklyReturn(Ad(SPY))

densitySPY = density(SPY)
plot(densitySPY,main="Kernel Density")

x = seq(min(SPY),max(SPY),length=300)
y = dnorm(x,mean=mean(SPY),sd = sd(SPY))
lines(x,y,col=2)

As you can see, for this data, the normal distribution is not a good fit for
data with excess kurtosis 6. dnorm is the pdf for the normal distribution,
it makes no sense to put normal random samples in it like you did in your
code: just put in an independent variable like you would for any function.

The problem with your code is that you hand it "curve" which expects a
function of x, but you give just a set of points with no reference to any x.
Better to work with plot directly and only use curve for getting a feel for
a function, rather than "official" plots. Lines can be used to add
additional lines to a plot.

Also, I'd almost guarantee that this error was reported when you called
curve; please mention that in your future posts.

If this doesn't solve your problem, use dput() to give a sample of your data
in a follow-up.

Michael

On Thu, Aug 25, 2011 at 12:45 PM, Newbie <lille_kn...@hotmail.com> wrote:

> Hi
>
> I have created the following plot over the empirical returns.. What I now
> want to do is to overlay a curve/line with the normal density as a
> comparison of the two. Does anyone know how to do this?
> (NB the last two lines are the problem, and are wrong, I know).
>
> Thank you in advance!
> Rikke
>
> http://r.789695.n4.nabble.com/file/n3768783/S%26P_500_spot_and_return_2010.csv
> S%26P_500_spot_and_return_2010.csv
>
> setwd("F:/Data til speciale/")
>
> marketdata <- read.csv(file="S&P 500 spot and return 2010.csv",
> header=TRUE,
> sep=";")
> returns <- marketdata[,7]
>
> ### Kernel density estimator ###########################
>
> x <- density (returns)
>
> plot(density(returns), main = "Kernel density")
> y <- rnorm(209,mean=0,sd=1)
> curve(dnorm(y,mean=mean(normal1),sd=sd(normal1)))
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Adding-a-normal-density-curve-over-the-empirical-curve-tp3768783p3768783.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org 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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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 via email to