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. Michael Weylandt
2011-Aug-25 18:37 UTC
[R] Adding a normal density curve over the empirical curve
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_knold@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. Michael Weylandt
2011-Aug-25 19:26 UTC
[R] Adding a normal density curve over the empirical curve
There's nothing wrong with leaving in buggy code when there's a bug that you just can't get (assuming it's part of your real code): I only ask for the error message because it saves us a little bit of time if you can let us know what errors you are getting. Anyways, best of luck, Michael On Thu, Aug 25, 2011 at 3:02 PM, Rikke Knold Christensen < lille_knold@hotmail.com> wrote:> Hi Michael > > Thank you for the quick response, it works perfectly now! > > You are quite right, I did get an error for the curve function, but I chose > to insert it in the code anyway, to show what I was trying to do (wrong) in > the y function. But you are right, it would have been more correct to show > what I was trying to do with the line(). > Thank you for explaining and helping me with this small problem > > Kinds Rikke > > ------------------------------ > From: michael.weylandt@gmail.com > Date: Thu, 25 Aug 2011 14:37:59 -0400 > Subject: Re: [R] Adding a normal density curve over the empirical curve > To: lille_knold@hotmail.com > CC: r-help@r-project.org > > 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_knold@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]]
Maybe Matching Threads
- Error message: object of type 'closure' is not subsettable
- Calibrating the risk free interest rate using nlminb
- [LLVMdev] Catching C++ exceptions, cleaning up, rethrowing
- mount_smbfs: unable to open connection: syserr = No such file or directory
- Fwd: Converting R to Sweave (Rnw)