I am trying to perform sliding correlation with window=11years.
Here's my code:
library(gtools)
var1<-rnorm(52,0.010,0.05)
var2<-rnorm(52,0.015,0.01)
dat<-merge(var1,var2)
dat$year<-seq(1961,2012,1)
rc<-running(dat$x,dat$y,fun=cor, width=11)
I want to plot the output as a simple line plot like this:
plot(rc,type="l")
My problem is the x-axis. How do I match the correlation value with the years? Something like adding a filler value "0"?
Any suggestions on how I can do this correctly in R.
I'll appreciate any help.
Many thanks in advance.
rc <- running(var1, var2, fun = cor, width = 11)
plot(1971:2012, rc, type="l")
Related
I'm new to R and am trying to plot a 3D surface. I thought it would be a pretty simple process as I have all my data in a nice table but can't figure out where I'm going wrong.
my_data2 (read in from Excel):
X1 X1.1 X1.21 X1.33 X1.46 X1.61 X1.77 X1.98 X2.14 X2.35
4e+05 291208737 291296846 291744988 292676157 304539662 347763047 346637087 352381080 361467196 334153676
5e+05 301234194 301322304 342042259 344633543 346394275 347763047 392216772 376048898 361467196 334153676
The first column is my row headers.
I've tried a few different R packages but am obviously missing something.
I thought it would be as simple as:
> r <- 1:nrow(my_data2)
> c <- 1:ncol(my_data2)
> z <- c(my_data2)
> contour3D(x=r, y=c, z=z, colvar=Volcano)
Error message displayed: "exactly one of the values 'x' 'y', or 'z' should be a matrix or one value"
I thought my z variable was a matrix!
Can anyone please help?
Tks
from the help page: "contour3D adds a contour in a 3-D plot." So I think you actually want something else. Maybe something like this:
library(plot3D)
z <- as.matrix(my_data2)
hist3D(z=z)
I am trying to simulate a signal in order to apply some methods of non-linear fittings, but I have some problems when plotting it.
x<-sample(seq(0,1,length.out = 1000),200)
y<-2*sin(4*pi*x)-6*abs(x-0.4)^(0.3)+2*exp(-30*(4*x-2)^2)+8*x+rnorm(200,0,0.5)
s<-2*sin(4*pi*x)-6*abs(x-0.4)^(0.3)+2*exp(-30*(4*x-2)^2)+8*x
plot(x,y)
lines(x,s,col="red")
The idea I want to have 200 observations uniformly sampled with an additive white noise term, and the I would like to plot this "perturbed" signal together with the original signal. (y and s respectively).
The fact is that if I use the code that I wrote I obtain as result something like:
Probably is such a simple thing, but I'm kinda stuck with this.
Any hint or suggestion will be greatly appreciated.
Lines are plotted sequentially, and you decided to randomly draw your X values, so x values sitting next to each other in x are not next to each other on the axis - hence the mess. Just sort it:
x<-sort(sample(seq(0,1,length.out = 1000),200))
y<-2*sin(4*pi*x)-6*abs(x-0.4)^(0.3)+2*exp(-30*(4*x-2)^2)+8*x+rnorm(200,0,0.5)
s<-2*sin(4*pi*x)-6*abs(x-0.4)^(0.3)+2*exp(-30*(4*x-2)^2)+8*x
plot(x,y)
lines(x,s,col="red")
Another way to do this on the fly mentioned by mickey is:
ord = order(x)
lines(x[ord], s[ord], col = 'red')
You need to reorder the x observations order in ascending order, you can do that by storing everything in a dataframe object and then ordering it:
x<-sample(seq(0,1,length.out = 1000),200)
df_p= data.frame(x)
df_p$y<-2*sin(4*pi*df_p$x)-6*abs(df_p$x-0.4)^(0.3)+2*exp(-30*(4*df_p$x-2)^2)+8*df_p$x+rnorm(200,0,0.5)
df_p$s<-2*sin(4*pi*df_p$x)-6*abs(df_p$x-0.4)^(0.3)+2*exp(-30*(4*df_p$x-2)^2)+8*df_p$x
df_p = df_p[order(df_p$x),]
plot(df_p$x,df_p$y)
lines(df_p$x, df_p$s,col="red")
Also if you want to avoid this step you can use the ggplot2 library:
p <- ggplot(df_p) + geom_point(aes(x = x,y= y)) + geom_line(aes(x=x,y=s,color='red'))
plot(p)
I am investigating Knn regression methods and later Kernel Smoothing.
I wish to demonstrate these methods using plots in R. I have generated a data set using the following code:
x = runif(100,0,pi)
e = rnorm(100,0,0.1)
y = sin(x)+e
I have been trying to follow a description of how to use "knn.reg" in 9.2 here:
https://daviddalpiaz.github.io/r4sl/k-nearest-neighbors.html#regression
grid2=data.frame(x)
knn10 = FNN::knn.reg(train = x, test = grid2, y = y, k = 10)
My predicted values seem reasonable to me but when I try to plot a line with them on top of my x~y plot I don't get what I'm hoping for.
plot(x,y)
lines(grid2$x,knn10$pred)
I feel like I'm missing something obvious and would really appreciate any help or advice you can offer, thank you for your time.
You just need to sort the x values before plotting the lines.
plot(x,y)
ORD = order(grid2$x)
lines(grid2$x[ORD],knn10$pred[ORD])
this is the first example of the ecdf() function in R:
F10 <- ecdf(rnorm(10)
plot(F10)
plot(F10, verticals = TRUE, do.points = FALSE)
Now I would like to "zoom" in the y-axis so that it only shows the interval of 0.9-1.0.
Does anybody know how to accomplish this? Thanks a lot in advance!
Thanks to rbm this is the easy solution:
plot(F10, ylim=c(0.90, 1.00))
I have a list of names sorted, like below:
ACVR2B
ADAM19
ADAM29
ADAM29
ADAMTS1
ADAMTS1
ADAMTS1
ADAMTS12
ADAMTS16
ADAMTS16
ADAMTS16
ADAMTS17
ADAMTS17
ADAMTSL1
ADCY10
would like to plot them as a histogram. It is very easy when these are values but with characters how can I do it in R or in open office?
Thank you
Try plotting the result of table(). The function table() computes the cross-tabulation frequency, which is exactly what you want.
set.seed(42)
x <- sample(letters, 100, replace = TRUE)
plot(table(x))
To plot the sorted values, try this:
z <- sort(table(x))
plot(z, xaxt="n", type="h")
axis(1, at=seq_along(z), names(z))
Given to what Andrie suggested: I did this:
Letter<-read.table("letters", header=T)
x <- sample(Letters, replace = F)
plot(sort(table(x)))
but the things is when, I want to plot in a descending order with only top 10 I miss out on the labels.
Can anyone suggest how to fix it and get only top 10.