I have instruction for R console:
plot(1:1000, some_function(1000,1/2,500, type='l')
Can someone explain to me what 1:1000 means? I tried to put some other input instead of 1:1000, for example 1:100 but then it won't work.
I understand that 1:1000 means it will iterate from 1 to 1000, but what does that mean as a plot argument?
Thanks in advance.
It means a vector with the elements from 1 to 1000; therefore, a 1000-element vector.
For the plot function, the first argument corresponds to the x coordinates of the points to be added. Hence, 1:1000 means that the data points will have x coordinates 1, 2, ..., 1000
Related
Consider this two‐dimensional random walk:
where, Zt, Wt, t = 1,2,3, … are independent and identically distributed standard normal
random variables.
I am having problems in finding a way to simulate and plot the sample path of (X,Y) for t = 0,1, … ,100. I was given a sample:
The following code is an example of the way I am used to plot random walks in R:
set.seed(13579)
r<-sample(c(-1,1),size=100,replace=T,prob=c(0.5,0.5))
r<-c(10,r))
(w<-cumsum(r))
w<-as.ts(w)
plot(w,main="random walk")
I am not very sure of how to achieve this.
The problem I am having is that this kind of codes has a more "simple" result, with a line that goes either up or down, -1 or +1:
while the plot I need to create also goes from left to right and viceversa.
Would you help me in correcting the code I know so that it fits my task/suggesting a smarterst way to go about it? It would be greatly appreciated.
Cheers!
Instead of using sample, you need to use rnorm(100) to draw 100 samples from a standard normal distribution. Since the walk starts at [0, 0], we need to append a 0 at the start and do a cumsum on the result, i.e. cumsum(c(0, rnorm(100))).
We want to do this for both the x and y variables, then plot. The whole thing can be done in a single line of code in base R:
plot(x = cumsum(c(0, rnorm(100))), y = cumsum(c(0, rnorm(100))), type = 'l')
I need to create something like a spider chart in R without using any libraries. That’s my code for now. It creates a figure with points number equal to the length of vector ‘a’. However, I’d like each point to be at the distance from the coordinates center equal to a respective number in a vector, for example one point at a distance 1, another at 2, so on. Is it possible to do so?
a <- 1:6
angle <- seq(0, 2*pi, (2*pi)/length(a))
x <- cos(angle)
y <- sin(angle)
plot(x, y,
type = "l")
See ?stars:
a <- 1:6
stars(matrix(a, nrow=1), scale=FALSE)
For future reference, using R's built-in help search would have found this with ??spider
I'm new user of R, and trying to generate a k-moving average graph with sine function which involves random number(in range [-0.5,+0.5]) noise.
So what I have to do is calculate a mean of consecutive (2*k+1) elements in noised-sine vector but however, the code with "HELP" below, it's not working as I expected... :(
The code seems to calculate the mean of 1 through (i-k)th element.
What's wrong with it? Help please!
set.seed(1)
x = seq(0,2*pi,pi/50)
sin_graph <- sin(x)
noise <- runif(101, -0.5, 0.5)
sin_noise <- sin_graph + noise
plot(x,sin_noise, ylim=c(-2,2))
lines(x,sin_graph, col="red")
k<-1
MA<-0
while (k<=1){
i <- k+1
MA_vector <- rep(NA, times=101)
while (i<=101-k){
MA_vector[i] <- mean(sin_noise[i-k:i+k]) #HELP!
i <- i+1
}
print(MA_vector)
plot(x, MA_vector, ylim=c(-2,2))
lines(x,sin_graph, col="red")
k<-k+1
}
As it stands, it's substracting a vector of k:i from i and then adding k. : takes precedent over mathematical operators. By using brackets (see code below), it evaluates i-k and i+k and creates a vector with min and max as results of the evaluations. I get another smooth function.
MA_vector[i] <- mean(sin_noise[(i-k):(i+k)])
everybody.
I am writing this question because I need your help to do a graphical representation in R. I would like to do a graphic as a coordinate system (two variables) and another variable representing the volume (size) of the points.
You can see an example:pxfolioplotbcgmatrix
Thank you!
Try ?symbols function
x <- 1:5
y <- 5:1
r <- seq(2, 10, 2)
symbols(x, y, circles=r, fg="white", bg="red")
This is a random data set being generated here for understanding and plotting a hierarchical cluster in R. I need to know the logic behind why the difference in the calls to rnorm for the x and y axis of the plot. Why y<-rnorm(12, mean=rep(c(1,2,1) when I would have expected mean=rep(c(1,2,3). Perhaps just the literal translation would help me.
set.seed(1234); par(mar=c(0,0,0,0)) ## par sets parameter mar (sets margin)
x<-rnorm(12, mean=rep(1:3,each=4),sd=0.2) ## repeat the vector 3 times
y<-rnorm(12, mean=rep(c(1,2,1),each=4),sd=0.2) ## ?????
plot(x,y,col="blue",pch=19,cex=2)
text(x+0.05,y+0.05,label=as.character(1:12))
Any help appreciated!
If you run your code, you get graphical output that looks something like this:
You can see that there are three clusters at three distinct mean x values (1, 2 and 3) but only two distinct y values (1 and 2, then 1 again). That's because the code for the y values has mean=rep(c(1,2,1),each=4). i.e. the rnorm function is generating twelve random y values, the first four of which have a mean of 1, the second four of which have a mean of 2 and the third four of which have a mean of 1.