Hello,
I would appreciate if someone could help me with this query. I would like to
plot a line chart of all of the points in a "for" loop. I would also
like
to plot the final point with a symbol (to show where the random walk ends).
Here is the code I am using:
Brownian.fn <- function(mu, sigma, T, N){
dt <- T/N
t <- c(rep(NA, N))
B1 <- c(rep(NA, N))
B2 <- c(rep(NA, N))
t[1] <- 0
B1[1] <- 0
B2[1] <- 0
for(j in 2:N){
t[j] <- t[j-1] + dt
B1[j] <- B1[j-1] + mu*dt + sigma*sqrt(dt)*rnorm(1, mean = 0, sd = 1)
B2[j] <- B2[j-1] + mu*dt + sigma*sqrt(dt)*rnorm(1, mean = 0, sd = 1)
}
plot(y = B2, x = B1, xlim = c(-2, 2),
ylim = c(-2, 2), type ="l", lty = 1, col=10)
}
Thank you for your help.
[[alternative HTML version deleted]]
Add this line of code to the end of your function, after the plot()
function.
points(B1[N], B2[N], pch=16, col=10)
Jean
`·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º>
Jean V. Adams
Statistician
U.S. Geological Survey
Great Lakes Science Center
223 East Steinfest Road
Antigo, WI 54409 USA
From:
Rebecca Gray <atlasrrg@gmail.com>
To:
r-help@r-project.org
Date:
07/27/2011 06:44 AM
Subject:
[R] plotting the ending point in a for loop
Sent by:
r-help-bounces@r-project.org
Hello,
I would appreciate if someone could help me with this query. I would like
to
plot a line chart of all of the points in a "for" loop. I would also
like
to plot the final point with a symbol (to show where the random walk
ends).
Here is the code I am using:
Brownian.fn <- function(mu, sigma, T, N){
dt <- T/N
t <- c(rep(NA, N))
B1 <- c(rep(NA, N))
B2 <- c(rep(NA, N))
t[1] <- 0
B1[1] <- 0
B2[1] <- 0
for(j in 2:N){
t[j] <- t[j-1] + dt
B1[j] <- B1[j-1] + mu*dt + sigma*sqrt(dt)*rnorm(1, mean = 0, sd = 1)
B2[j] <- B2[j-1] + mu*dt + sigma*sqrt(dt)*rnorm(1, mean = 0, sd = 1)
}
plot(y = B2, x = B1, xlim = c(-2, 2),
ylim = c(-2, 2), type ="l", lty = 1, col=10)
}
Thank you for your help.
[[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.
[[alternative HTML version deleted]]
Don't really undertsand, are you looking for points(B2[N], B1[N]) ? Uwe Ligges On 27.07.2011 11:59, Rebecca Gray wrote:> Hello, > > I would appreciate if someone could help me with this query. I would like to > plot a line chart of all of the points in a "for" loop. I would also like > to plot the final point with a symbol (to show where the random walk ends). > Here is the code I am using: > > Brownian.fn<- function(mu, sigma, T, N){ > > dt<- T/N > > t<- c(rep(NA, N)) > B1<- c(rep(NA, N)) > B2<- c(rep(NA, N)) > > t[1]<- 0 > B1[1]<- 0 > B2[1]<- 0 > > > for(j in 2:N){ > > t[j]<- t[j-1] + dt > B1[j]<- B1[j-1] + mu*dt + sigma*sqrt(dt)*rnorm(1, mean = 0, sd = 1) > > B2[j]<- B2[j-1] + mu*dt + sigma*sqrt(dt)*rnorm(1, mean = 0, sd = 1) > > } > > > plot(y = B2, x = B1, xlim = c(-2, 2), > ylim = c(-2, 2), type ="l", lty = 1, col=10) > > } > > Thank you for your help. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.