Skip to content
Prev 15588 / 398500 Next

Help for Power analysis

"Ass.Prof. Nikom Thanomsieng" wrote:
Given the code you wrote is correct (didn't checked it, but looks OK in
the hurry):
What does this (above) line mean?

I changed your code a little bit (loop and several lines are not
neccessary).
I put it into a function and added the lines to automatically draw the
lines it seems you want to be drawn (see below). Hope this helps...
power.analysis <- function(x1, x2, my.power = 0.8, 
    Ntotal1 = 900, alpha = 0.05){
  nc <- cbind(x1)
  nr <- rbind(x2)
  data1 <- rbind(x1, x2)
  chi2 <- chisq.test(data1, correct=FALSE)$statistic
  Ntotal <- sum(data1)
  df <- ncol(nc-1) * nrow(nr-1)
  w <- ifelse(df == 1, sqrt(chi2 / Ntotal), sqrt(chi2 / (chi2+Ntotal)))
  chicrit <- qchisq(1-alpha, df)
  n <- 1:Ntotal1
  ncp <- w^2 * n
  power <- 1 - (pchisq(chicrit , df, ncp))
  plot(n, power, type = "l", col = "red", lwd = 1, panel.first =
grid(10,10), 
    main = "Power as a function of Sample Size", xlab = "Sample Size", 
    ylab = "Power")
  min.samp <- which(power >= my.power)[1]
  segments(min.samp, my.power, min.samp,   0, col = "pink")
  segments(min.samp, my.power,   0, my.power, col = "pink")
  mtext("Chi-Square", side = 3, line = 0.35,
    outer = FALSE, at = mean(par("usr")[1:2]), cex = 1, font = 4)
  samplesize <- cbind(n, ncp, power)
  return(samplesize)
}

power.analysis(c(9, 6), c(6, 6))


Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._