Thank you very much Petr,

I believe I have fixed my inquiry to not use floating points in my cycle as
you pointed out and to use the list structure to keep my results. I am
still at a loss as to how to run the multiple loops. I have tried quite a
few different strategies but my failure seems to illustrate that my
understanding of how the loops will run is nonexistent.

I would like to simultaneously let the following 4 things vary:

z1 <- rnorm(ss,mean=400, sd=70) and z2 <- rnorm(ss,mean=450, sd=90) by ss
<- seq(5,9,by=1) which yields 5 6 7 8 9

r <- cc by cc <- seq(-0.5,0.5, by=0.25) which yields -0.50 -0.25 0.00 0.25
0.50

dev1 <- oo1 by oo1 <- seq(-10,10, by=5) which yields -10 -5 0 5 10

dev2 <- oo2 by oo2 <- seq(0,20, by=5) which yields 0 5 10 15 20

I tried to run the loops that would vary each of these above conditions
with the looped code attached below and failed very badly.

Thank you Petr and all,

Best,

Thomas

###################################SINGLE RUN
CODE####################################

lll <- vector(mode = "list", length = 10)

names(lll) <- c("mat1", "mat2", "mat3", "mat4", "cut1", "cut2", "out3a",
"out3b", "out4a", "out4b")

z1 <- rnorm(20,mean=400, sd=70)

z2 <- rnorm(20,mean=450, sd=90)

cor <- runif(1,min=0.4, max=0.6)

X <- z1

Y = cor*z1+(1-cor)*z2

lll[["mat1"]] <- cbind(X,Y)

dev1 <- sample(-40:40, 1, replace=T)

lll[["cut1"]] <- mean(X) + dev1

dev2 <- sample(12:54, 1, replace=T)

lll[["cut2"]] <- mean(X) + dev1 + dev2

X2 <- X-lll[["cut1"]]

Y2 <- Y-lll[["cut2"]]

c3 <- cor(X2,Y2)

lll[["mat2"]] <-cbind(X2,Y2)

a11 <- ifelse( X < lll[["cut1"]] & Y < lll[["cut2"]], 1, 0)

a12 <- ifelse( X < lll[["cut1"]] & Y >= lll[["cut2"]], 1, 0)

a21 <- ifelse( X >= lll[["cut1"]] & Y < lll[["cut2"]], 1, 0)

a22 <- ifelse( X >= lll[["cut1"]] & Y >= lll[["cut2"]], 1, 0)

lll[["mat3"]] <-matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[["mat4"]] <-matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[["out3a"]] <- mcnemar.test(lll[["mat3"]], correct=FALSE)

lll[["out3b"]] <- mcnemar.test(lll[["mat3"]], correct=TRUE)

lll[["out4a"]] <- chisq.test(lll[["mat4"]], correct = FALSE)

lll[["out4b"]] <- chisq.test(lll[["mat4"]], correct = TRUE)

print(lll)

capture.output(print(lll), file = "C:/Chi_Square_fix/temp.txt", append =
TRUE)

######################################LOOPED
CODE#####################################

lll <- vector(mode = "list", length = 10)

names(lll) <- c("mat1", "mat2", "mat3", "mat4", "cut1", "cut2", "out3a",
"out3b", "out4a", "out4b")

ss <- seq(5,9,by=1)

cc <- seq(-0.5,0.5, by=0.25)

oo1 <- seq(-10,10, by=5)

oo2 <- seq(0,20, by=5)

for(i in ss) {

for (j in cc) {

for (k in oo1) {

for (l in oo2) {

z1 <- rnorm(ss,mean=400, sd=70)

z2 <- rnorm(ss,mean=450, sd=90)

r <- cc

X <- z1

Y = r*z1+(1-r)*z2

lll[["mat1"]] <- cbind(X,Y)

dev1 <- oo1

lll[["cut1"]] <- mean(X) + dev1

dev2 <- oo2

lll[["cut2"]] <- mean(X) + dev1 + dev2

X2 <- X-lll[["cut1"]]

Y2 <- Y-lll[["cut2"]]

c3 <- cor(X2,Y2)

lll[["mat2"]] <-cbind(X2,Y2)

a11 <- ifelse( X < lll[["cut1"]] & Y < lll[["cut2"]], 1, 0)

a12 <- ifelse( X < lll[["cut1"]] & Y >= lll[["cut2"]], 1, 0)

a21 <- ifelse( X >= lll[["cut1"]] & Y < lll[["cut2"]], 1, 0)

a22 <- ifelse( X >= lll[["cut1"]] & Y >= lll[["cut2"]], 1, 0)

lll[["mat3"]] <-matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[["mat4"]] <-matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[["out3a"]] <- mcnemar.test(lll[["mat3"]], correct=FALSE)

lll[["out3b"]] <- mcnemar.test(lll[["mat3"]], correct=TRUE)

lll[["out4a"]] <- chisq.test(lll[["mat4"]], correct = FALSE)

lll[["out4b"]] <- chisq.test(lll[["mat4"]], correct = TRUE)

print(lll)

capture.output(print(lll), file = "C:/Chi_Square_fix/temp.txt", append =
TRUE)

}

}

}

}
On Feb 1, 2013 2:01 AM, "PIKAL Petr" <petr.pikal@precheza.cz> wrote:

> Hi
>
> see inline
>
> > -----Original Message-----
> > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-
> > project.org] On Behalf Of staysafe23
> > Sent: Friday, February 01, 2013 1:01 AM
> > To: r-help@r-project.org
> > Subject: [R] Nested loop and output help
> >
> > Hello Everyone,
> >
> > My name is Thomas and I have been using R for one week. I recently
> > found your site and have been able to search the archives of posts.
> > This has given me some great information that has allowed me to craft
> > an initial design to an inquiry I would like to make into the breakdown
> > of McNemar's test. I have read an intro to R manual and the posting
> > guides and hope I am not violating them with this post. If so I will
> > re-ask my question in the proper format.
> >
> > I have succeeded in writing a loop to vary one condition of my inquiry
> > but I am unable to understand how to vary the remaining three
> > conditions, each with its own for loop. Each time I try to do so I fail
> > miserably. Here is my current code :
> >
> > n <- seq(5,10,by=1)
> >
> > for(i in n) {
> >
> > z1 <- rnorm(n,mean=400, sd=70)
> >
> > z2 <- rnorm(n,mean=450, sd=90)
> >
> > cor <- runif(1,min=0.4, max=0.6)
> >
> > X <- z1
> >
> > Y = cor*z1+(1-cor)*z2
> >
> > mat1 <- cbind(X,Y)
> >
> > dev1 <- sample(-40:40, 1, replace=T)
> >
> > cut1 <- mean(X) + dev1
> >
> > dev2 <- sample(12:54, 1, replace=T)
> >
> > cut2 <- mean(X) + dev1 + dev2
> >
> > X2 <- X-cut1
> >
> > Y2 <- Y-cut2
> >
> > c3 <- cor(X2,Y2)
> >
> > mat2 <-cbind(X2,Y2)
> >
> > a11 <- ifelse( X < cut1 & Y < cut2, 1, 0)
> >
> > a12 <- ifelse( X < cut1 & Y >= cut2, 1, 0)
> >
> > a21 <- ifelse( X >= cut1 & Y < cut2, 1, 0)
> >
> > a22 <- ifelse( X >= cut1 & Y >= cut2, 1, 0)
> >
> > mat3 <-matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)
> >
> > mat4 <-matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)
> >
> > out3a <- mcnemar.test(mat3, correct=FALSE)
> >
> > out3b <- mcnemar.test(mat3, correct=TRUE)
> >
> > out4a <- chisq.test(mat4, correct = FALSE)
> >
> > out4b <- chisq.test(mat4, correct = TRUE)
> >
> > print(mat1)
> >
> > print(mat2)
> >
> > print(cut1)
> >
> > print(cut2)
> >
> > print(mat3)
> >
> > print(out3a)
> >
> > print(out3b)
> >
> > print(mat4)
> >
> > print(out4a)
> >
> > print(out4b)
> >
> > }
> >
> > .
>
> Use list structure for keeping such results.
>
> lll<-list()
> for(j in 1:5) {
> lll[[j]] <- list()
> for( i in 1:3) lll[[j]][[i]]<-rnorm(10)
> }
>
> after population of a list you can print it as a whole or only part. Here
> is an example with your code.
>
> n <- seq(5,10,by=1)
>
> lll <- vector(mode = "list", length = 10)
> names(lll) <- c("mat1", "mat2", "mat3", "mat4", "cut1", "cut2", "out3a",
> "out3b", "out4a", "out4b")
>
> n <- seq(5,10,by=1)
>
> for(i in n) {
> z1 <- rnorm(n,mean=400, sd=70)
> z2 <- rnorm(n,mean=450, sd=90)
> cor <- runif(1,min=0.4, max=0.6)
> X <- z1
> Y = cor*z1+(1-cor)*z2
>
> lll[["mat1"]] <- cbind(X,Y)
> dev1 <- sample(-40:40, 1, replace=T)
>
> lll[["cut1"]] <- mean(X) + dev1
> dev2 <- sample(12:54, 1, replace=T)
>
> lll[["cut2"]] <- mean(X) + dev1 + dev2
> X2 <- X-cut1
> Y2 <- Y-cut2
> c3 <- cor(X2,Y2)
>
> lll[["mat2"]] <-cbind(X2,Y2)
> a11 <- ifelse( X < cut1 & Y < cut2, 1, 0)
> a12 <- ifelse( X < cut1 & Y >= cut2, 1, 0)
> a21 <- ifelse( X >= cut1 & Y < cut2, 1, 0)
> a22 <- ifelse( X >= cut1 & Y >= cut2, 1, 0)
>
> lll[["mat3"]] <-matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)
> lll[["mat4"]] <-matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)
> lll[["out3a"]] <- mcnemar.test(mat3, correct=FALSE)
> lll[["out3b"]] <- mcnemar.test(mat3, correct=TRUE)
> lll[["out4a"]] <- chisq.test(mat4, correct = FALSE)
> lll[["out4b"]] <- chisq.test(mat4, correct = TRUE)
> }
>
> print(lll)
>
> >
> > Other than the sample size of the random draws I would like to nest for
> > loops for cor, dev1, and dev2 according to the following sequences
> > respectively:
> >
> > cor <- seq(-0.5,0.5, by=0.25)
>
> do not use floating points in cycle.
>
> better to use
>
> for (k in 1:n) {
> cc <- cor[k]
> make computation with cc
> }
>
>
> >
> > dev1 <- seq(-100,100, by=10)
> >
> > dev2 <- seq(12,54, by=10)
> >
> > .
> >
> > After doing so I would like to put each matrix and their respective
> > tests into a text file so that I can examine the results. I would like
> > to put the results in a .txt file each time the loop finishes one case.
> > I would like to append this text file with subsequent matrices and
> > results rendered by each iteration of the nested for loop.  I have seen
> > some very nice examples of output that R can render. I would like to
> > simply display each matrix and their tests.
>
> maybe R2HTML or latex in Hmisc package can
>
> Regards
> Petr
>
> >
> > Thank you to all the teachers and students on this forum. The only
> > reason I have been able to craft this inquiry is due to the questions
> > and answers I have found through searching the archive. Thank you
> > kindly for your assistance and for freely sharing your knowledge.
> >
> > Best wishes,
> > Thomas
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
>

	[[alternative HTML version deleted]]

