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)
Related
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)
R version: 3.4.2
I'm using rugarch and mgarch to spec and fit model with DCC to my data. The model is generated successfully, however I'm unable to generate the plots. Here's a snippet of my code:
library(rugarch)
library(rmgarch)
da=read.table("d-msft3dx0113.txt",header=T)
MSFT.ret = da[,3]
GSPC.ret = da[,6]
MSFT.GSPC.ret = cbind(MSFT.ret,GSPC.ret)
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(garchOrder = c(1,1),
model = "sGARCH"),
distribution.model = "norm")
dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),
dccOrder = c(1,1),
distribution = "mvnorm")
dcc.fit = dccfit(dcc.garch11.spec, data = MSFT.GSPC.ret)
dcc.fcst = dccforecast(dcc.fit, n.ahead=100)
plot(dcc.fcst)
When I call for plot, I get this error:
plot(dcc.fcst)
Make a plot selection (or 0 to exit):
Conditional Mean Forecast (vs realized returns)
Conditional Sigma Forecast (vs realized |returns|)
Conditional Covariance Forecast
Conditional Correlation Forecast
EW Portfolio Plot with forecast conditional density VaR limits
Selection: 1
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
I then give it a new plot area:
plot.new()
plot(dcc.fcst)
Which gives me this unhelpful plot:
Selection1Plot
I have the same question, too. I don't know why plot(dcc.fic) cannot work. So I do it manually to extract the correlation and covariance. rcov and rcor are two functions to extract what we need.
plot(rcov(dcc.fit)[1,2,], type = "l", col = "blue",
main = "Conditional Covariance", xlab = "Time",
ylab = "Covariance")
plot(rcor(dcc.fit)[1,2,], type = "l", col = "purple",
main = "Conditional Correlation", xlab = "Time",
ylab = "Correlation")
I tried to create a fitted line through the graph, but it doesn't show at all, it just shows the graph itself. What am I doing wrong here? Check my code below.
cor.merged <- cor.merged[order(cor.merged$No.florets), ]
plot(vis.rate ~ No.florets, data = cor.merged[cor.merged$line == 'M',],
main = "Nectar production vs visitation rate",
xlab = "No.florets", ylab = "Visitation rate",
pch = 19)
m1 <- lm(cor.merged$vis.rate[cor.merged$line == 'M'] ~
poly(cor.merged$No.florets[cor.merged$line == 'M'], 2, raw = T))
summary(m1)
lines(cor.merged$No.florets[cor.merged$line == 'M'] ~
predict(m1),
col = 'red', lwd = 3, type = 'c')
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!
This question already has answers here:
Shading a kernel density plot between two points.
(5 answers)
Closed 7 years ago.
I've written code to plot density data for variations of an A/B test. I'd like to improve the visual by shading (with the fill being slightly transparent) the area below each curve. I'm currently using matplot, but understand ggplot might be a better option.
Any ideas? Thanks.
# Setup data frame - these are results from an A/B experiment
conv_data = data.frame(
VarNames = c("Variation 1", "Variation 2", "Variation 3") # Set variation names
,NumSuccess = c(1,90,899) # Set number of successes / conversions
,NumTrials = c(10,100,1070) # Set number of trials
)
conv_data$NumFailures = conv_data$NumTrials - conv_data$NumSuccess # Set number of failures [no conversions]
num_var = NROW(conv_data) # Set total number of variations
plot_col = rainbow(num_var) # Set plot colors
get_density_data <- function(n_var, s, f) {
x = seq(0,1,length.out=100) # 0.01,0.02,0.03...1
dens_data = matrix(data = NA, nrow=length(x), ncol=(n_var+1))
dens_data[,1] = x
# set density data
for(j in 1:n_var) {
# +1 to s[], f[] to ensure uniform prior
dens_data[,j+1] = dbeta(x, s[j]+1, f[j]+1)
}
return(dens_data)
}
density_data = get_density_data(num_var, conv_data$NumSuccess, conv_data$NumFailures)
matplot(density_data[,1]*100, density_data[,-1], type = "l", lty = 1, col = plot_col, ylab = "Probability Density", xlab = "Conversion Rate %", yaxt = "n")
legend("topleft", col=plot_col, legend = conv_data$VarNames, lwd = 1)
This produces the following plot:
# Setup data frame - these are results from an A/B experiment
conv_data = data.frame(
VarNames = c("Variation 1", "Variation 2", "Variation 3") # Set variation names
,NumSuccess = c(1,90,899) # Set number of successes / conversions
,NumTrials = c(10,100,1070) # Set number of trials
)
conv_data$NumFailures = conv_data$NumTrials - conv_data$NumSuccess # Set number of failures [no conversions]
num_var = NROW(conv_data) # Set total number of variations
plot_col = rainbow(num_var) # Set plot colors
get_density_data <- function(n_var, s, f) {
x = seq(0,1,length.out=100) # 0.01,0.02,0.03...1
dens_data = matrix(data = NA, nrow=length(x), ncol=(n_var+1))
dens_data[,1] = x
# set density data
for(j in 1:n_var) {
# +1 to s[], f[] to ensure uniform prior
dens_data[,j+1] = dbeta(x, s[j]+1, f[j]+1)
}
return(dens_data)
}
density_data = get_density_data(num_var, conv_data$NumSuccess, conv_data$NumFailures)
matplot(density_data[,1]*100, density_data[,-1], type = "l",
lty = 1, col = plot_col, ylab = "Probability Density",
xlab = "Conversion Rate %", yaxt = "n")
legend("topleft", col=plot_col, legend = conv_data$VarNames, lwd = 1)
## and add this part
for (ii in seq_along(plot_col))
polygon(c(density_data[, 1] * 100, rev(density_data[, 1] * 100)),
c(density_data[, ii + 1], rep(0, nrow(density_data))),
col = adjustcolor(plot_col[ii], alpha.f = .25))
Was able to answer own question with:
df = as.data.frame(t(conversion_data))
dfs = stack(df)
ggplot(dfs, aes(x=values)) + geom_density(aes(group=ind, colour=ind, fill=ind), alpha=0.3)