I have this plot which i generate it from this code:
m <- bcea(e=effects,c=costs, ref=2, interventions=treatments, Kmax=50000)
The plot is:
evi.plot(m)
Now, i need to export this evpi.plot(m) in an excel file, not the jpeg created, but the data along with it, i mean what created the X and Y axis.
I've been using something like this but it's not for this case
write.table( thresholds, 'clipboard', sep='\t', row.names=FALSE, col.names=FALSE )
In the documentation for function bcea from package BCEA you can see the structure of your object:
Value
An object of the class "bcea" containing the following elements
n.sim Number of simulations produced by the Bayesian model
n.comparators Number of interventions being analysed
...
k
The vector of values for the grid approximation of the willingness to pay
...
evi The vector of values for the Expected Value of Information, as a
function of the willingness to pay
And if you look at the function definition of evi.plot you will see that your x and y-values are the elements named k and evi:
> evi.plot
function (he)
{
options(scipen = 10)
plot(he$k, he$evi, t = "l", xlab = "Willingness to pay",
ylab = "EVPI", main = "Expected Value of Information")
if (length(he$kstar) > 0) {
points(rep(he$kstar, 3), c(-10000, he$evi[he$k == he$kstar]/2,
he$evi[he$k == he$kstar]), t = "l", lty = 2, col = "dark grey")
points(c(-10000, he$kstar/2, he$kstar), rep(he$evi[he$k ==
he$kstar], 3), t = "l", lty = 2, col = "dark grey")
}
}
<environment: namespace:BCEA>
So:
res <- cbind(m$k, m$evi)
write.table(res, file="bcea.csv", sep=',', row.names=FALSE, col.names=FALSE )
Related
In R I would like to create several plots in a for-loop. The y-axis label is supposed to exhibit a subscript, in which the subscript text should vary along with the loop-iterator.
For subscripting a label, I previously used "expression". However, as you can see in the minimal example, the subscript in the expression cannot be indexed the way I thought it would (instead of printing "1", "2", "3" it simply prints "i"). Do you have an idea on how to fix this (either by using the expression function or any other text function able to produce subscripts)?
Minimal code:
# minimal example code
Data = matrix(ncol = 4, nrow = 1000)
colnames(Data) = c("time", "k1", "k2", "k3")
Data[,1] = seq(0.1,100,0.1)
Data[,2] = sin(Data[,1])
Data[,3] = cos(Data[,1])
Data[,4] = tan(Data[,1])
for(i in 1:3) {
plot(Data[,1], Data[,(1+i)], type = "l", lwd = 2, xlab = "time", ylab = expression("k" [i]))
}
Thank you!
Use bquote. Stolen from this SO:
Subscripts in plots in R
for(i in 1:3) {
plot(Data[,1], Data[,(1+i)], type = "l", lwd = 2, xlab = "time", ylab = bquote(k[.(i)]))
}
It has very strange syntax of: bquote(WORD [ . (OBJECT) ]. Note that WORD is not quoted and the dot. I believe the . is referring to what environment to go find OBJECT.
So I'm doing a meta-analysis using the meta.for package in R. I am preparing figures for publication in a scientific journal and i would like to add p-values to my forest plots but with scientific annotation formatted as x10-04 rather than standard
e-04
However the argument ilab in the forest function does not accept expression class objects but only vectors
Here is an example :
library(metafor)
data(dat.bcg)
## REM
res <- rma(ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg,
measure = "RR",
slab = paste(author, year, sep = ", "), method = "REML")
# MADE UP PVALUES
set.seed(513)
p.vals <- runif(nrow(dat.bcg), 1e-6,0.02)
# Format pvalues so only those bellow 0.01 are scientifically notated
p.vals <- ifelse(p.vals < 0.01,
format(p.vals,digits = 3,scientific = TRUE,trim = TRUE),
format(round(p.vals, 2), nsmall=2, trim=TRUE))
## Forest plot
forest(res, ilab = p.vals, ilab.xpos = 3, order = "obs", xlab = "Relative Risk")
I want the scientific notation of the p-values to be formatted as x10-04
All the answers to similar questions that i've seen suggest using expression() but that gives Error in cbind(ilab) : cannot create a matrix from type 'expression' which makes sense because the help file on forest specifies that the ilab argument should be a vector.
Any ideas on how I can either fix this or work around it?
A hacky solution would be to
forest.rma <- edit(forest.rma)
Go to line 574 and change
## line 574
text(ilab.xpos[l], rows, ilab[, l], pos = ilab.pos[l],
to
text(ilab.xpos[l], rows, parse(text = ilab[, l]), pos = ilab.pos[l],
fix your p-values and plot
p.vals <- gsub('e(.*)', '~x~10^{"\\1"}', p.vals)
forest(res, ilab = p.vals, ilab.xpos = 3, order = "obs", xlab = "Relative Risk")
I was trying to draw some lines in the same plot. The x factor is determined by a date and the y factor by a number. I initially load the data, store it in a list and save the min and max values for the date:
stocks <- list()
stocks.min <- 0
stocks.max <- 0
stocks.min.date <- NULL
stocks.max.date <- NULL
for (name in names(files))
{
stocks[[name]] <- read.csv(files[[name]], sep=";")
# Convert to date in R
stocks[[name]]$Date <- as.Date(stocks[[name]]$Date, "%d/%m/%Y")
# Sets max value for ylim in the plotting
if (stocks.max < max(stocks[[name]]$Close))
{
stocks.max <- max(stocks[[name]]$Close)
}
# Sets the date value for the xlim in the plot
if (is.null(stocks.min.date) || min(stocks[[name]]$Date) < stocks.min.date)
{
stocks.min.date <- min(stocks[[name]]$Date)
}
if (is.null(stocks.max.date) || max(stocks[[name]]$Date) > stocks.max.date)
{
stocks.max.date <- max(stocks[[name]]$Date)
}
}
After that I create an empty plot using the values from above:
plot(0, xlab="Time", ylab="Closing Prices", main="Stock Values",
xlim=c(stocks.min.date, stocks.max.date), ylim=c(stocks.min, stocks.max))
And then I add the lines with the data:
for (name in names(stocks))
{
lines(x=stocks[[name]]$Date, y=stocks[[name]]$Close, col=colors[[name]], type="l",
lwd=2)
}
When the graph is plotted, the data is correctly displayed, but it shows the date as numbers instead of dates in the x axis as seen in the image below:
How can I correct this issue?
I would strongly suggest using a normalized series to plot the stocks data you have. quantmod helps a lot here. It solves two purposes -
Get the x-axis labels as dates.
Normalize series so that you can view any number of series without worrying about the orders of their absolute values (~67 for INR, ~1120 for KRW, so on...)
This is what I generally use for my purposes.
library(quantmod)
tickers <- c('GOOG', 'MSFT', 'AAPL', 'AMZN')
getSymbols(tickers, src = 'yahoo', from = '2015-01-01')
normalise <- function(x) x/as.numeric(x)[1] - 1
chart_theme <- chart_theme()
chart_theme$col$line.col <- "red"
chart_Series(normalise(Cl(GOOG)), theme = chart_theme)
add_TA(normalise(Cl(MSFT)), on = 1, col = "black", lty = 1)
add_TA(normalise(Cl(AMZN)), on = 1, col = "blue", lty =1)
add_TA(normalise(Cl(AAPL)), on = 1, col = "darkgreen", lty =2)
Hope this helps.
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)