by mariuskempe on 3/16/12, 11:40 PM with 25 comments
by steve19 on 3/17/12, 2:11 AM
Yet I love writing code in it. So much can be done in so little code. I am always amazed at how little code I write to accomplish a task.
The RStudio IDE ( http://rstudio.org/ ) is a very pleasant environment to write code in.
by jacobolus on 3/17/12, 1:25 AM
I’d rewrite this example as something like:
num.steps <- 1000
num.walks <- 100
step.std.dev <- 0.03
start.value <- 15
rand.row <- function() rnorm(num.steps, 1, step.std.dev)
walk <- function () cumprod(rand.row()) * start.value
all.walks <- t(replicate(100, walk()))
plot(colMeans(all.walks), type = "l")
[I’m not an R guy, so that might not be the most typical style ever, but you get the idea...]by svdad on 3/17/12, 12:34 AM
by danielharan on 3/17/12, 1:19 AM
by Estragon on 3/17/12, 12:33 PM
by choffstein on 3/17/12, 4:10 AM
In my mind, Matlab's (Octave's) array based programming makes sense. This? This does nothing that I expected it to do!
replicate seems to pretty randomly takes a function. Is that an R thing? I think what confuses me most is that there is nothing about this syntax that tells me that "cumprod (rnorm (1000, 1, 0.03))" hasn't already been evaluated! I could not, for the life of me, figure out why replicate didn't just create 100 exact copies. For example, why does replicate(...) evaluate, but the internals don't? This is driving me crazy!
by radikalus on 3/17/12, 3:29 AM
How often do you want to generate random walks of this type where the variance of the process isn't dependent on its current level?
Observe that, as you increase the standard deviation of the random normal (to even .1), your "random walk" always walks to zero.
I don't mean to be a beady-eyed-pterodactyl but, as cool as clever one-liners sometimes are, often they solve toy problems. I say this as someone who loves R and uses it everyday and constantly is forced to brute-force with ugly inlined Rcpp code. (Which is fine)
by psb217 on 3/17/12, 1:57 AM
plot(mean((cumprod(randn(100,1000) .* 0.03) .* 15))
Well, that is assuming one wants a line plot of the mean value of the "location" at each time point across the population of walks. Personally, I find R's rather idiosyncratic approaches to data handling and function wrangling a bit hard to digest.
by mbq on 3/17/12, 12:37 AM
by swah on 3/17/12, 12:34 AM
by the_cat_kittles on 3/17/12, 1:19 AM