Hi, Can someone help me out to create a (for?) loop for the following procedure: x=rnorm(250,0,0.02) library(timeSeries) x=timeSeries(x) P=1000 loss=-P*x loss v95=quantile(loss,0.95) v95 I would like to generate 100 000 v95 values. Also, can anybody name a source where I could read up basic programming in R? Thank you. -- View this message in context: http://r.789695.n4.nabble.com/Loop-noob-question-tp3721500p3721500.html Sent from the R help mailing list archive at Nabble.com.
This is a textbook of when NOT to use a loop in R: rather make a function that does what you want and use the replicate function to do it repeatedly. f <- function(){ return(-1000*quantile(rnorm(250,0,0.2),0.95) } x = replicate(1e5,f()) There are your desired numbers. Some general coding principles: firstly, you don't need to convert to time series: quantile immediately undoes that so you've just wasted the time doing the coercions each direction. Secondly, the quantiles of c*X are exactly c times the quantiles of X so you can cut down on the multiplications needed by doing the -1000 multiplication after quantilization. Specific to R: don't use loops unless entirely necessary. (And it's hardly ever necessary) -- replicate is great for repeated operations, apply is great for looping "over" thins. More broadly, what do you intend to do with the v95 values? There are probably much more efficient ways to get the desired numbers or even closed form results. I believe this idea is widely studied as VaR in finance. Feel free to write back if we can be of more help. Hope this helps, Michael Weylandt On Fri, Aug 5, 2011 at 11:35 AM, UnitRoot <akhussanov@gmail.com> wrote:> Hi, > Can someone help me out to create a (for?) loop for the following > procedure: > > x=rnorm(250,0,0.02) > library(timeSeries) > x=timeSeries(x) > P=1000 > loss=-P*x > loss > v95=quantile(loss,0.95) > v95 > > I would like to generate 100 000 v95 values. > Also, can anybody name a source where I could read up basic programming in > R? > Thank you. > > -- > View this message in context: > http://r.789695.n4.nabble.com/Loop-noob-question-tp3721500p3721500.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]]
Hey, no problem! We all have to start somewhere, welcome to R! The structure of the for loop is as follows: First lets define the number you want, say >vector.size = 100000 First allocate an empty vector to store the results, you could do this like so> V95.Vector<-c()But I recommend you do it like so> V95.Vector=rep(NA,vector.size)Basically what this does is make a vector of NA's (of length n) for you to store into, that way if something goes wrong in your loop it will be easier to catch. This is better to use when you know the size of your vector from the start( in my opinion), which you do. Now, lets allocate a variable for the counting, say 'loop.count' So we have:>for( loop.count in 1:vector.size) #Count each time from 1 to the number wedefined, vector.size { x=rnorm(250,0,0.02) #generate data library(timeSeries) #Note that you can call this before the loop, because once you read it in, its there to stay, this will make your loop much slower. x=timeSeries(x) #Apply sum function to data P=1000 #You can and should also call this before the loop if it doesn't change. loss=-P*x #calc v95=quantile(loss,0.95) #Quantile V95.Vector[loop.count]=v95 #Store into index of this loop. } And that should be it, as far as relevant reading Peter Daalgard's Introductory Statistics with R is very good if you do not know other programming languages. Crowleys R Book is the Bible as it were, and is very very good. Electronic copies are available. Good luck! Ken On Fri, Aug 5, 2011 at 11:35 AM, UnitRoot <akhussanov@gmail.com> wrote:> Hi, > Can someone help me out to create a (for?) loop for the following > procedure: > > x=rnorm(250,0,0.02) > library(timeSeries) > x=timeSeries(x) > P=1000 > loss=-P*x > loss > v95=quantile(loss,0.95) > v95 > > I would like to generate 100 000 v95 values. > Also, can anybody name a source where I could read up basic programming in > R? > Thank you. > > -- > View this message in context: > http://r.789695.n4.nabble.com/Loop-noob-question-tp3721500p3721500.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]]