Reputation: 21
This is my code and my final graph result. But I don't know how to make the line smoother. Are there any suggestions? I tried loess and spline but I don't know how I should apply them on my code.
p2 <- seq(.1587, .18144, .00756)
np2 <- length(p2)
power <- seq(.4, .9, .1)
npower <- length(power)
samsize <- array(numeric(np2*npower), dim = c(np2,npower))
for (i in 1:npower){
for (j in 1:np2){
result <- power.prop.test(n = NULL, p1 =.1511, p2 = p2[j],
sig.level = .05, power = power[i],
alternative = c("two.sided","one.sided"),
strict = FALSE, tol = .Machine$double.eps^.25)
samsize[j,i] <- ceiling(result$n)
}
}
xrange <- range(p2)
yrange <- round(range(samsize))
colors <- rainbow(length(power))
plot(xrange, yrange, type = "n",
xlab = "Target Percentage (p2)",
ylab = "Sample Size (n)")
for (i in 1:npower){
lines(p2, samsize[,i], lwd = 2, col=colors[i])
}
abline(h=seq(0,50000,5000), v=seq(xrange[1],xrange[2], .002),lty=20,col="gray89")
title("Sample Size Estimation for Proportion Test")
legend("topright", title= "Power", as.character(power),
fill=colors)
Thank you!
Upvotes: 1
Views: 128
Reputation: 780
You just need to convert your array to a dataframe that ggplot can handle. Then you can use geom_smooth.
df <- data.frame(samsize)
colnames(df) <- as.character(power)
df <- tidyr::gather(df)
df$p2 <- p2
ggplot() +
geom_smooth(data=df, aes(x=p2, y=value, color=key)) +
scale_color_discrete(name = 'Power') +
labs(x = 'Target Percentage (p2)', y = 'Sample Size (n)', title="Sample Size Estimation for Proportion Test") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5))
Upvotes: 1
Reputation: 50668
You could increase the number of points for p2
at which you calculate the sample size.
For example, if you set
p2 <- seq(.1587, .18144, length.out = 100)
then you get
Upvotes: 2