I have two dataframes which are RMA normalized as
set.seed(1)
X <- data.frame(matrix(rnorm(20), nrow=10))
Y <- data.frame(matrix(rnorm(20), nrow=10))
The columns are the gene expression levels and the rows are the genes. How to plot the density distribution of covariance of gene expression levels of X and distribution of covariance of gene expression levels Y in a single plot. It is something like this but I would like to study the distribution of the entire dataframe than columns.enter link description here
I tried using
plot (density(X), col="red",ylim=c(0,3.5),xlim=c(-1,2))
lines (density(Y), col="green")
But I get an error
Error in density.default() : argument 'x' must be numeric
I am still not sure, I understand you correctly.
set.seed(1)
X <- data.frame(matrix(rnorm(20), nrow=10))
Y <- data.frame(matrix(rnorm(20), nrow=10))
#CV
d1 <- density(sapply(X, function(x) sd(x)/mean(x)))
d2 <- density(sapply(Y, function(x) sd(x)/mean(x)))
plot(d1, ylim=c(0,max(d1$y,d2$y)), xlim=range(d1$x,d2$x), col="green", xlab="", main="")
par(new=TRUE)
plot(d2, ylim=c(0,max(d1$y,d2$y)), xlim=range(d1$x,d2$x), col="red", xlab="", main="")
par(new=FALSE)
#covariance
d3 <- density(cov(X))
d4 <- density(cov(Y))
plot(d3, ylim=c(0,max(d3$y,d4$y)), xlim=range(d3$x,d4$x), col="green", xlab="", main="")
par(new=TRUE)
plot(d4, ylim=c(0,max(d3$y,d4$y)), xlim=range(d3$x,d4$x), col="red", xlab="", main="")
par(new=FALSE)
Related
Given two variables, x and y, I run a dynlm regression on the variables and would like to plot the fitted model against one of the variables and the residual on the bottom showing how the actual data line differs from the predicting line. I've seen it done before and I've done it before, but for the life of me I can't remember how to do it or find anything that explains it.
This gets me into the ballpark where I have a model and two variables, but I can't get the type of graph I want.
library(dynlm)
x <- rnorm(100)
y <- rnorm(100)
model <- dynlm(x ~ y)
plot(x, type="l", col="red")
lines(y, type="l", col="blue")
I want to generate a graph that looks like this where you see the model and the real data overlaying each other and the residual plotted as a separate graph on the bottom showing how the real data and the model deviate.
This should do the trick:
library(dynlm)
set.seed(771104)
x <- 5 + seq(1, 10, len=100) + rnorm(100)
y <- x + rnorm(100)
model <- dynlm(x ~ y)
par(oma=c(1,1,1,2))
plotModel(x, model) # works with models which accept 'predict' and 'residuals'
and this is the code for plotModel,
plotModel = function(x, model) {
ymodel1 = range(x, fitted(model), na.rm=TRUE)
ymodel2 = c(2*ymodel1[1]-ymodel1[2], ymodel1[2])
yres1 = range(residuals(model), na.rm=TRUE)
yres2 = c(yres1[1], 2*yres1[2]-yres1[1])
plot(x, type="l", col="red", lwd=2, ylim=ymodel2, axes=FALSE,
ylab="", xlab="")
axis(1)
mtext("residuals", 1, adj=0.5, line=2.5)
axis(2, at=pretty(ymodel1))
mtext("observed/modeled", 2, adj=0.75, line=2.5)
lines(fitted(model), col="green", lwd=2)
par(new=TRUE)
plot(residuals(model), col="blue", type="l", ylim=yres2, axes=FALSE,
ylab="", xlab="")
axis(4, at=pretty(yres1))
mtext("residuals", 4, adj=0.25, line=2.5)
abline(h=quantile(residuals(model), probs=c(0.1,0.9)), lty=2, col="gray")
abline(h=0)
box()
}
what you're looking for is resid(model). Try this:
library(dynlm)
x <- 10+rnorm(100)
y <- 10+rnorm(100)
model <- dynlm(x ~ y)
plot(x, type="l", col="red", ylim=c(min(c(x,y,resid(model))), max(c(x,y,resid(model)))))
lines(y, type="l", col="green")
lines(resid(model), type="l", col="blue")
(Please note: I'm using R for only two days now.)
I have a dataset data that looks like this:
plot(data, pch=20, xlim=c(-2,3), ylim=c(-1,2))
I'm using the mixsmsn package to fit a mixture of bivariate skew-normal distributions:
sn2 <- smsn.mmix(data, nu=3, g=2, get.init=TRUE, criteria=TRUE, group=TRUE, family="Skew.normal", error=1e-08, iter.max=10000)
I can plot it like this (why pch=20 doesn't work?):
mix.contour(data, sn2, pch=20, xlim=c(-2,3), ylim=c(-1,2), levels=c(0.1,0.25,0.5))
How can I achieve the following?
I'd want to draw a contour separately for each component at half its height. That is, say it's a mixture distribution of the form p f_1(x,y) + (1-p) f_2(x,y) (f_i being the pdf of the _i_th skew-normal component); I'd want to draw (on a scatter plot) a contour of the f_1 component at half its height, and a second contour related to f_2 at half its height; I'd like the result to look like this:
Using the fMultivar package, I came up with this:
X <- data
sn2 <- smsn.mmix(X, nu=3, g=2, get.init=TRUE, criteria=TRUE, group=TRUE, family="Skew.normal", error=1e-08, iter.max=10000)
mu1 <- sn2$mu[[1]]
sigma1 <- sn2$Sigma[[1]]
alpha1 <- c(sn2$shape[[1]][1], sn2$shape[[1]][2])
p1 <- sn2$pii[[1]]
mu2 <- sn2$mu[[2]]
sigma2 <- sn2$Sigma[[2]]
alpha2 <- c(sn2$shape[[2]][1], sn2$shape[[2]][2])
p2 <- sn2$pii[[2]]
N <- 101
x <- seq(min(X[, 1]), max(X[, 1]), l=N)
y <- seq(min(X[, 2]), max(X[, 2]), l=N)
u <- grid2d(x, y)$x
v <- grid2d(x, y)$y
XY <- cbind(u, v)
Z1 <- matrix(p1*dmsn(XY, mu1, sigma1, alpha1), ncol=N)
Z2 <- matrix(p2*dmsn(XY, mu2, sigma2, alpha2), ncol=N)
c1 <- 0.5*max(Z1)
c2 <- 0.5*max(Z2)
plot(X, pch=20, xlim=c(-2,3), ylim=c(-1,2))
contour(x, y, Z1, add=TRUE, col="red", lwd=3, levels=c(c1), labels="")
contour(x, y, Z2, add=TRUE, col="green", lwd=3, levels=c(c2), labels="")
I've made a foresplot in r using the metafor package with the following code:
res <- metafor::rma(cohens_d, variance, data = my_data)
par(mar=c(3.4,0,0,0))
par(cex=2.5, font=4)
metafor::forest.rma(res, alim=c(-3.75, 3.75), xlab = "Cohen's D with 95% CI",
slab = my_data$Paper)
which gives me the image:
I'd like to flip the x axis, meaning have the negative on the right. Any ideas on how to do this?
Thank you!
Okay, this is a really ridiculous hack, but it works. Basically, you have to draw the plot twice, once to add the points (with reversed signs) and once to add the annotations (without the reversed signs).
library(metafor)
dat <- get(data(dat.bcg))
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
res <- rma(yi, vi, data=dat)
### default plot
forest(res, xlim=c(-8,8))
### trick into reverse x-axis
forest(res, xaxt="n", transf=function(x) -1*x, annotate=FALSE, xlim=c(-8,8), xlab="Log Relative Risk")
axis(side=1, at=seq(-3, 3, 1), labels=seq(3,-3,-1))
par(new=TRUE)
forest(res, xaxt="n", xlim=c(-8,8), col="white", border="white", pch=NA, lty="blank", efac=NA, xlab="", slab=NA, mlab=NA)
par(new=FALSE)
I would simply just flip the estimated coefficients of the model (b component of the rma class) prior to plotting:
res <- metafor::rma(cohens_d, variance, data = my_data)
res$b <- -res$b
I have a dataset called dataframe (a 2d table) and a best fit curve as:
scatter.smooth(dataframe, xlab="", ylab="")
What code would I need to realize and evaluate (get numerical value of) a Y value on that best fit curve at a single x value?
Try
set.seed(1)
dataframe <- data.frame(x=runif(100), y=runif(100))
scatter.smooth(dataframe, xlab="", ylab="")
res <- with(dataframe, loess.smooth(x, y, evaluation = 200))
lengths(res)
# x y
# 200 200
x <- 0.5
y <- res$y[res$x>=x][1]
points(x, y, col="blue", pch = 19, cex=2)
I want to plot multiple histograms in R which do not show frequency, but the density instead:
A <- rnorm(100)
B <- rnorm(100)
hist1 <- hist(A,prob=TRUE,breaks=30)
hist2 <- hist(B,prob=TRUE,breaks=30)
plot(hist1, col="red",lty=0, xlim=c(-4,4))
plot(hist2, col="blue", lty=0, xlim=c(-4,4), add=TRUE, main="Example")
lines(density(A))
However, my 'prob=TRUE' option apparently doesn't go through when plotting the objects. Can someone explain to me what I am doing wrong?
leave the prob=T out of the hist() command
hist1 <- hist(A,breaks=30)
hist2 <- hist(B,freq=F,breaks=30)
And put freq=F into the plot command.
plot(hist1, col="red",lty=0, xlim=c(-4,4),freq=F)
plot(hist2, col="blue", lty=0, xlim=c(-4,4), add=TRUE, main="Example",freq=F)