On Feb 1, 2013, at 7:47 AM, John Sorkin wrote:

Windows 7, R 2.12.1
Colleagues,
I am trying to understand the n.for.2means function. The code below is a copy of the function (renamed to n.for.2means.js). I have inserted a single line of code towards the bottom of the function which uses the cat function to print the value of n1. You will note the value (preceded by stars) is printed as 1. The function (1) prints a lot of output without any instructions in the function to print anything (other than the cat statement I added), and when it prints (2) reports the value of n1 to be 2!. I have two questions, (i) how is the function printing when there is no code to print and (ii) how is n1 which equals 1 being reported as 2? I suspect there is something fundamental about R that I don't know.

1) on my machine the output from the cat() call is:

******n1*******= 1

2) All of the "output without any instructions in the function to print anything" is just the value of the list object from the function. Unless you return values using the `invisible` function, any user define function executed at the console will print its value. That is standard interactive session behavior. So one gets after the the cat output:

$mu1
[1] 0

$mu2
[1] 8

$sd1
[1] 1.666667

$sd2
[1] 1.666667

$alpha
[1] 0.05

$n1
[1] 1

$n2
[1] 1

$power
[1] 0.8

$ratio
[1] 1

$table
  mu1 mu2      sd1      sd2 n1 n2
1   0   8 1.666667 1.666667  1  1

attr(,"class")
[1] "n.for.2means" "list"


--
David.

Thank you for the help.
John


library(epicalc)
n.for.2means.js <- function (mu1, mu2, sd1, sd2, ratio = 1, alpha = 0.05, power = 0.8)
{
n1 <- (sd1^2 + sd2^2/ratio) * (qnorm(1 - alpha/2) - qnorm(1 - power))^2/(mu1 - mu2)^2
 n1 <- round(n1)
 n2 <- ratio * n1
 if (length(alpha) == 1) {
   alpha1 <- NULL
 }
 else {
   alpha1 <- alpha
 }
 if (length(power) == 1) {
   power1 <- NULL
 }
 else {
   power1 <- power
 }
 if (length(ratio) == 1) {
   ratio1 <- NULL
 }
 else {
   ratio1 <- ratio
 }
 table1 <- cbind(mu1, mu2, sd1, sd2, n1, n2, alpha1, power1,
                 ratio1)
 colnames(table1)[colnames(table1) == "alpha1"] <- "alpha"
 colnames(table1)[colnames(table1) == "power1"] <- "power"
 colnames(table1)[colnames(table1) == "ratio1"] <- "n2/n1"
 table1 <- as.data.frame(table1)
 cat("******n1*******=",n1,"\n")
 returns <- list(mu1 = mu1, mu2 = mu2, sd1 = sd1, sd2 = sd2,
alpha = alpha, n1 = n1, n2 = n2, power = power, ratio = ratio,
                 table = table1)
 class(returns) <- c("n.for.2means", "list")
 returns
}
n.for.2means.js(0,8,10/6,10/6)


John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
Confidentiality Statement:
This email message, including any attachments, is for ...{{dropped:15}}

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to