I am trying to graph my matrix columns using plot but it looks like I made a mistake somewhere. I found the example code online so I'm not entirely sure how to troubleshoot it.
d <- seq(from = 0.3, to = 0.5, by = 0.01)
nd <- length(d)
p <- seq(from = 0.6, to = 0.8, by = 0.1)
np <- length(p)
samplesizes <- data.frame(matrix(data=0, nrow=nd, ncol=np))
names(samplesizes) <- p
rownames(samplesizes) <- d
for (i in 1:np){
for (j in 1:nd){
result <- power.t.test(n=NULL, d = d[j], sig.level = .05,
power = p[i], alternative = "one.sided")
samplesizes[j,i] <- result$n
}
}
#-----------------------------------------------------
xrange <- range(d)
yrange <- round(range(samplesizes))
colors <- rainbow(length(p))
plot(samplesizes, xrange, yrange, type="n", xlab="Sample size (n)",
ylab="Effect Size (d)", las = 1)
for (i in 1:np){
lines(s, effectsizes[,i], type="l", lwd=2, col=colors[i])
}
legend("topright", title="Power", as.character(p), fill=colors)
I am trying to get something similar to this example graph but with sample on the Y axis and effect size on the X axis.
CodePudding user response:
Maybe you are looking for this -
d <- seq(from = 0.3, to = 0.5, by = 0.01)
nd <- length(d)
p <- seq(from = 0.6, to = 0.8, by = 0.1)
np <- length(p)
samplesizes <- data.frame(matrix(data=0, nrow=nd, ncol=np))
names(samplesizes) <- p
rownames(samplesizes) <- d
for (i in 1:np){
for (j in 1:nd){
result <- power.t.test(n=NULL, d = d[j], sig.level = .05,
power = p[i], alternative = "one.sided")
samplesizes[j,i] <- result$n
}
}
xrange <- range(d)
yrange <- round(range(samplesizes))
colors <- rainbow(length(p))
plot(xrange, yrange, type="n", xlab="Sample size (n)",
ylab="Effect Size (d)", las = 1)
for (i in 1:np){
lines(d, samplesizes[,i], type="l", lwd=2, col=colors[i])
}
legend("topright", title="Power", as.character(p), fill=colors)