I would like to plot several forecasts on the same plot in different colours, however, the scale is off.
I'm open to any other methods.
reproducible example:
require(forecast)
# MAKING DATA
data <- c(3.86000, 19.55810, 19.51091, 20.74048, 20.71333, 29.04191, 30.28864, 25.64300, 23.33368, 23.70870 , 26.16600 ,27.61286 , 27.88409 , 28.41400 , 24.81957 , 24.60952, 27.49857, 32.08000 , 29.98000, 27.49000 , 237.26150, 266.35478, 338.30000, 377.69476, 528.65905, 780.00000 )
a.ts <- ts(data,start=c(2005,1),frequency=12)
# FORECASTS
arima011_css =stats::arima(x = a.ts, order = c(0, 1, 1), method = "CSS") # css estimate
arima011_forecast = forecast(arima011_css, h=10, level=c(99.5))
arima321_css =stats::arima(x = a.ts, order = c(3, 2, 1), method = "CSS") # css estimate
arima321_forecast = forecast(arima321_css, h=10, level=c(99.5))
# MY ATTEMPT AT PLOTS
plot(arima321_forecast)
par(new=T)
plot(arima011_forecast)
Here is something similar to #jay.sf but using ggplot2.
library(ggplot2)
autoplot(a.ts) +
autolayer(arima011_forecast, series = "ARIMA(0,1,1)", alpha = 0.5) +
autolayer(arima321_forecast, series = "ARIMA(3,2,1)", alpha = 0.5) +
guides(colour = guide_legend("Model"))
Created on 2020-05-19 by the reprex package (v0.3.0)
You could do a manual plot using a sequence of dates.
rn <- format(seq.Date(as.Date("2005-01-01"), by="months", length.out=12*3), "%Y.%m")
Your ARIMAs you'll need as.matrix form.
arima321_mat <- as.matrix(as.data.frame(arima321_forecast))
arima011_mat <- as.matrix(as.data.frame(arima011_forecast))
Some colors with different alpha=.
col.1 <- rainbow(2, ,.7)
col.2 <- rainbow(2, ,.7, alpha=.2)
For the CIs use polygon.
plot(data, type="l", xlim=c(1, length(rn)), ylim=c(0, 3500), xaxt="n", main="Forecasts")
axis(1, axTicks(1), labels=F)
mtext(rn[(seq(rn)-1) %% 5 == 0], 1, 1, at=axTicks(1))
lines((length(data)+1):length(rn), arima321_mat[,1], col=col.1[1], lwd=2)
polygon(c(27:36, 36:27), c(arima321_mat[,2], rev(arima321_mat[,3])), col=col.2[1],
border=NA)
lines((length(data)+1):length(rn), arima011_mat[,1], col=col.1[2], lwd=3)
polygon(c(27:36, 36:27), c(arima011_mat[,2], rev(arima011_mat[,3])), col=col.2[2],
border=NA)
legend("topleft", legend=c("ARIMA(3,2,1)", "ARIMA(0,1,1)"), col=col.1, lwd=2, cex=.9)
Edit: To avoid the repetition of lines and polygon calls, you may unite them using Map.
mats <- list(arima321_mat, arima011_mat) ## put matrices into list
plot(.)
axis(.)
mtext(.)
Map(function(i) {
lines((length(data)+1):length(rn), mats[[i]][,1], col=col.1[i], lwd=2)
polygon(c(27:36, 36:27), c(mats[[i]][,2], rev(mats[[i]][,3])), col=col.2[i], border=NA)
}, 1:2)
legend(.)
require(forecast)
data <- c(3.86000, 19.55810, 19.51091, 20.74048, 20.71333, 29.04191, 30.28864, 25.64300, 23.33368, 23.70870 , 26.16600 ,27.61286 , 27.88409 , 28.41400 , 24.81957 , 24.60952, 27.49857, 32.08000 , 29.98000, 27.49000 , 237.26150, 266.35478, 338.30000, 377.69476, 528.65905, 780.00000 )
a.ts <- ts(data,start=c(2005,1),frequency=12)
arima011_css =stats::arima(x = a.ts, order = c(0, 1, 1), method = "CSS") # css estimate
arima011_forecast = predict(arima011_css, n.ahead = 2)$pred
arima321_css =stats::arima(x = a.ts, order = c(3, 2, 1), method = "CSS") # css estimate
arima321_forecast = predict(arima321_css, n.ahead = 2)$pred
plot(a.ts, type = "o", xlim = c(2005, 2007.5) , ylim = c(-1, 1200) , ylab = "price" ,main = "2 month Forecast")
range = c(2007+(3/12), 2007+(4/12)) # adding the dates for the prediction
lines(y = arima011_forecast , x = range , type = "o", col = "red")
lines(y = arima321_forecast, x = range , type = "o", col = "blue")
I'm trying to study this protein interaction net. I need to do a plot of the degree distribution of the edges (last lines of the code), but I'm not able to make a trend line and the slope (last line of the code). Can someone help me please?
install.packages("igraph")
library("igraph")
tfile<-tempfile()
download.file("http://pdg.cnb.uam.es/pazos/tmp/Yeast_int.txt", tfile)
datosraw <- subset(read.delim(tfile, sep="\t", header=F, stringsAsFactors = F), !is.na(V3) & V3!="" & V3!="METHOD")
names(datosraw)<-c("orf1","orf2","method")
datos<-split(datosraw,datosraw$method)
df.y2h <- graph.data.frame(d = datos$Y2H[1:5125,c(1,2)], directed = FALSE)
deg.dist.df.y2h <- degree.distribution(df.y2h)
plot(deg.dist.df.y2h, xlab="k", ylab="P(k)", main="Y2H")
plot(deg.dist.df.y2h, xlab="log k", ylab="log P(k)", main="Y2H", log = "xy", asp)
abline(lm(deg.dist.df.y2h))
You are placing abline on the plot done on log10-log10 scale. Here's one way of doing it
Your code
library("igraph")
tfile<-tempfile()
download.file("http://pdg.cnb.uam.es/pazos/tmp/Yeast_int.txt", tfile)
datosraw <- subset(read.delim(tfile, sep="\t", header=F, stringsAsFactors = F), !is.na(V3) & V3!="" & V3!="METHOD")
names(datosraw)<-c("orf1","orf2","method")
datos<-split(datosraw,datosraw$method)
df.y2h <- graph.data.frame(d = datos$Y2H[1:5125,c(1,2)], directed = FALSE)
The rest can be wrapped in to a helper function
plot_degree_distribution = function(graph) {
# calculate degree
d = degree(graph, mode = "all")
dd = degree.distribution(graph, mode = "all", cumulative = FALSE)
degree = 1:max(d)
probability = dd[-1]
# delete blank values
nonzero.position = which(probability != 0)
probability = probability[nonzero.position]
degree = degree[nonzero.position]
# plot
plot(probability ~ degree, log = "xy", xlab = "Degree (log)", ylab = "Probability (log)",
col = 1, main = "Degree Distribution")
abline(lm(log10(probability) ~ log10(degree)))
}
plot_degree_distribution(df.y2h)
I'm trying to do a plot with a trend line but I'm obtaining this: https://imgur.com/a/xeAI1
How can I fix the trend line to the plot??
Thanks in advance
library("igraph")
tfile<-tempfile()
download.file("http://pdg.cnb.uam.es/pazos/tmp/Yeast_int.txt", tfile)
datosraw <- subset(read.delim(tfile, sep="\t", header=F, stringsAsFactors = F), !is.na(V3) & V3!="" & V3!="METHOD")
names(datosraw)<-c("orf1","orf2","method")
datos<-split(datosraw,datosraw$method)
df.y2h <- graph.data.frame(d = datos$Y2H[1:5125,c(1,2)], directed = FALSE)
plot_degree_distribution = function(graph) {
# calculate degree
d = degree(graph, mode = "all")
dd = degree.distribution(graph, mode = "all", cumulative = FALSE)
degree = 1:max(d)
probability = dd[-1]
# delete blank values
nonzero.position = which(probability != 0)
probability = probability[nonzero.position]
degree = degree[nonzero.position]
# plot
plot(probability ~ degree, log = "xy", xlab = "Degree (log)", ylab = "Probability (log)",
col = 1, main = "Degree Distribution")
mod <- (lm(log10(probability) ~ log10(degree)))
abline((mod), col="red")
coef(mod)[2]
}
plot_degree_distribution(df.y2h)
This is what i have tried
setwd("C:\\Ds")
reddit <- mtcars
reddit <- na.omit(reddit)
View(reddit)
cars<-reddit[,c(9,23)]
na.omit(cars)
col1<-cars[,1]/1000000
col2<-cars[,2]/1000000
z<-cbind(col1,col2)
cl<-means(z,4)
options(scipen = 10)
format(z,scientific=FALSE)
plot(z, col = cl$cluster,color=TRUE,las=1,xlab = "Gross in millions",ylab = "Budget in millions")
points(cl$centers, col = 1:2, pch = 18, cex = 2.5)
I want to create something like this:
In ccf, When I give the option of plot=TRUE, I will get a plot that gives me 95% confidence interval cut-offs for my cross-correlations at each lag.
My question is, if I want to use a 90% confidence level, how can I do this? Thanks.
I believe that by setting Plot=T, ccf function passes its results to acf and then to plot.acf function. plot.acf is where confidence interval is calculated. You can see it with the ":::" device
stats:::plot.acf
You should see something simmilar:
function (x, ci = 0.95, type = "h", xlab = "Lag", ylab = NULL .....etc.
I suggest you set plot=F in ccf function, then use plot function seperately, changing confidence interval (ci). You can do so by using this code:
plot(x, ci = 0.90, type = "h", xlab = "Lag", ylab = NULL,
ylim = NULL, main = NULL,
ci.col = "blue", ci.type = c("white", "ma"),
max.mfrow = 6, ask = Npgs > 1 && dev.interactive(),
mar = if(nser > 2) c(3,2,2,0.8) else par("mar"),
oma = if(nser > 2) c(1,1.2,1,1) else par("oma"),
mgp = if(nser > 2) c(1.5,0.6,0) else par("mgp"),
xpd = par("xpd"),
cex.main = if(nser > 2) 1 else par("cex.main"),
verbose = getOption("verbose"),
...)
LINK:
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/plot.acf.html
Since you havent provided any actual example data, i will show you an example with two common time series: recruit.dat and soi.dat. Just run this script line by line and try to understand what each line does.
#import data from web:
soi = ts(scan("http://anson.ucdavis.edu/~shumway/soi.dat"), start=1950, frequency=12)
rec = ts(scan("http://anson.ucdavis.edu/~shumway/recruit.dat"), start=1950, frequency=12)
#run simple ccf function with plot=F
ccfvalues =ccf (soi, rec, plot=F)
#now run a plot function, with the desired confidence interval. Simple as that!
plot (ccfvalues, ci=0.90, type = "h", xlab = "Lag", ylab = NULL,ylim = NULL, main = NULL,ci.col = "blue", ci.type = c("white", "ma"),max.mfrow = 6, ask = Npgs > 1 && dev.interactive(),mar = if(nser > 2) c(3,2,2,0.8) else par("mar"),oma = if(nser > 2) c(1,1.2,1,1) else par("oma"),mgp = if(nser > 2) c(1.5,0.6,0) else par("mgp"),xpd = par("xpd"),cex.main = if(nser > 2) 1 else par("cex.main"),verbose = getOption("verbose"))
Replace the soi and recruit data with your own and you are all set!
Hope it works!