Re: [R] Nested structure data simulation

2019-05-21 Thread Linus Chen
Dear varin sacha

On Sun, May 19, 2019 at 5:14 PM varin sacha via R-help
 wrote:
>
> Dear Boris,
>
> Great  But what about Mark in your R code ? Don't we have to precise in 
> the R code that mark ranges between 1 to 6 (1 ; 1.5 ; 2 ; 2.5 ; 3 ; 3.5 ; 4 ; 
> 4.5 ; 5 ; 5.5 ; 6) ?

I think Boris is just setting up a framework for you. It is up to you
to decide the actual values. :)
You maybe want to create a MyData object, with the method Boris has
shown, but filling the Mark field with random numbers.

Cheers,
Lei

>
> By the way, to fit a linear mixed model, I use lme4 package and then the lmer 
> function works with the variables like in this example here below :
>
> library(lme4)
> mm=lmer(Mark ~Gender + (1 | School / Class), data=Dataset)
>
> With your R code, how can I write the lmer function to make it work ?
>
> Best,
> S.
>
>
>
>
>
>
>
> Le dimanche 19 mai 2019 à 15:26:39 UTC+2, Boris Steipe 
>  a écrit :
>
>
>
>
>
> Fair enough - there are additional assumptions needed, which I make as 
> follows:
>   - each class has the same size
>   - each teacher teaches the same number of classes
>   - the number of boys and girls is random within a class
>   - there are 60% girls  (just for illustration that it does not have to be 
> equal)
>
>
> To make the dependencies explicit, I define them so, and in a way that they 
> can't be inconsistent.
>
> nS <- 10# Schools
> nTpS <- 5  # Teachers per School
> nCpT <- 2  # Classes per teacher
> nPpC <- 20  # Pupils per class
> nS * nTpS * nCpT * nPpC == 2000  # Validate
>
>
> mySim <- data.frame(School  = paste0("s", rep(1:nS, each = nTpS*nCpT*nPpC)),
> Teacher = paste0("t", rep(1:(nTpS*nS), each = nCpT*nPpC)),
> Class  = paste0("c", rep(1:(nCpT*nTpS*nS), each = nPpC)),
> Gender  = sample(c("boy", "girl"),
> (nS*nTpS*nCpT*nPpC),
> prob = c(0.4, 0.6),
> replace = TRUE),
> Mark= numeric(nS*nTpS*nCpT*nPpC),
> stringsAsFactors = FALSE)
>
>
> Then you fill mySim$Mark with values from your linear mixed model ...
>
> mySim$Mark[i] <- simMarks(mySim[i])  # ... or something equivalent.
>
>
> All good?
>
> Cheers,
> Boris
>
>
>
> > On 2019-05-19, at 08:05, varin sacha  wrote:
> >
> > Many thanks to all of you for your responses.
> >
> > So, I will try to be clearer with a larger example. Te end of my mail is 
> > the more important to understand what I am trying to do. I am trying to 
> > simulate data to fit a linear mixed model (nested not crossed). More 
> > precisely, I would love to get at the end of the process, a table (.txt) 
> > with columns and rows. Column 1 and Rows will be the 2000 pupils and the 
> > columns the different variables : Column 2 = classes ; Column 3 = teachers, 
> > Column 4 = schools ; Column 5 = gender (boy or girl) ; Column 6 = mark in 
> > Frecnh
> >
> > Pupils are nested  in classes, classes are nested in schools. The teacher 
> > are part of the process.
> >
> > I want to simulate a dataset with n=2000 pupils, 100 classes, 50 teachers 
> > and 10 schools.
> > - Pupils n°1 to pupils n°2000 (p1, p2, p3, p4, ..., p2000)
> > - Classes n°1 to classes n°100 (c1, c2, c3, c4,..., c100)
> > - Teachers n°1 to teacher n°50 ( t1, t2, t3, t4, ..., t50)
> > - Schools n°1 to chool n°10 (s1, s2, s3, s4, ..., s10)
> >
> > The nested structure is as followed :
> >
> > -- School 1 with teacher 1 to teacher 5 (t1, t2, t3, t4 and t5) with 
> > classes 1 to classes 10 (c1, c2, c3, c4, c5, c6, c7, c8,c9,c10), pupils n°1 
> > to pupils n°200 (p1, p2, p3, p4,..., p200).
> >
> > -- School 2 with teacher 6 to teacher 10, with classes 11 to classes 20, 
> > pupils n°201 to pupils n°400
> >
> > -- and so on
> >
> > The table (.txt) I would love to get at the end is the following :
> >
> >ClassTeacherSchoolgenderMark
> > 1  c1t1s1boy5
> > 2  c1t1s1boy5.5
> > 3  c1t1s1girl4.5
> > 4  c1t1s1girl6
> > 5  c1t1s1boy  3.5
> > 6  .....
> >
> > The first 20 rows with c1, with t1, with s1, gender (randomly slected) and 
> > mark (andomly selected) from 1 to 6
> > The rows 21 to 40 with c2 with t1 with s1
> > The rows 41 to 60 with c3 with t2 with s1
> > The rows 61 to 80 with c4 with t2 with s1
> > The rows 81 to 100 with c5 with t3 with s1
> > The rows 101 to 120 with c6 with t3 with s1
> > The rows 121 to 140 with c7 with t4 with s1
> > The rows 141 to 160 with c8 with t4 with s1
> > The rows 161 to 180 with c9 with t5 with s1
> > The rows 181 to 200 with c10 with t5 with s1
> >
> > The rows 201 to 220 with c11 with t6 with s2
> > 

Re: [R] Nested structure data simulation

2019-05-21 Thread Linus Chen
Dear varin sacha,

I think it will help us help you, if you give a clearer description of
what exactly you want.

I assume the situation is that you know what a data structure you
want, but do not know
how to conveniently create such structure.
And that is where others can help you.
So, please, describe the wanted data structure more thoroughly,
ideally with example.

Thanks,
Lei

On Sat, May 18, 2019 at 10:04 PM varin sacha via R-help
 wrote:
>
> Dear Boris,
>
> Yes, top-down, no problem. Many thanks, but in your code did you not forget 
> "teacher" ? As a reminder teacher has to be nested with classes. I mean the 
> 50 pupils belonging to C1 must be with (teacher 1) T1, the 50 pupils 
> belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so on.
>
> Best,
>
>
> Le samedi 18 mai 2019 à 16:52:48 UTC+2, Boris Steipe 
>  a écrit :
>
>
>
>
>
> Can you build your data top-down?
>
>
>
> schools <- paste("s", 1:6, sep="")
>
> classes <- character()
> for (school in schools) {
>   classes <- c(classes, paste(school, paste("c", 1:5, sep=""), sep = "."))
> }
>
> pupils <- character()
> for (class in classes) {
>   pupils <- c(pupils, paste(class, paste("p", 1:10, sep=""), sep = "."))
> }
>
>
>
> B.
>
>
>
> > On 2019-05-18, at 09:57, varin sacha via R-help  
> > wrote:
> >
> > Dear R-Experts,
> >
> > In a data simulation, I would like a balanced distribution with a nested 
> > structure for classroom and teacher (not for school). I mean 50 pupils 
> > belonging to C1, 50 other pupils belonging to C2, 50 other pupils belonging 
> > to C3 and so on. Then I want the 50 pupils belonging to C1 with T1, the 50 
> > pupils belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and 
> > so on. The school don’t have to be nested, I just want a balanced 
> > distribution, I mean 60 pupils in S1, 60 other pupils in S2 and so on.
> > Here below the reproducible example.
> > Many thanks for your help.
> >
> > ##
> > set.seed(123)
> > # Génération aléatoire des colonnes
> > pupils<-1:300
> > classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)  
> > teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)  
> > school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T)
>
> > ##
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested structure data simulation

2019-05-21 Thread Linus Chen
Dear varin sacha,

Not very sure what you want, but will the following help a little?

tmp <- rep(c("C1","C2","C3","C4","C5","C6"), 50) # make a character
vector, with 50 "C1", 50 "C2", ...
classroom <- tmp[sample(1:300)] # make a random permutation.

Certainly you may also make it into one line:
classroom <- rep(c("C1","C2","C3","C4","C5","C6"), 50) [sample(1:30)]

Best,
Lei Chen

On Sat, May 18, 2019 at 3:57 PM varin sacha via R-help
 wrote:
>
> Dear R-Experts,
>
> In a data simulation, I would like a balanced distribution with a nested 
> structure for classroom and teacher (not for school). I mean 50 pupils 
> belonging to C1, 50 other pupils belonging to C2, 50 other pupils belonging 
> to C3 and so on. Then I want the 50 pupils belonging to C1 with T1, the 50 
> pupils belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so 
> on. The school don’t have to be nested, I just want a balanced distribution, 
> I mean 60 pupils in S1, 60 other pupils in S2 and so on.
> Here below the reproducible example.
> Many thanks for your help.
>
> ##
> set.seed(123)
> # Génération aléatoire des colonnes
> pupils<-1:300
> classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)   
> teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)   
> school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T)
> ##
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested structure data simulation

2019-05-19 Thread Boris Steipe
My mental model for such a simulation is that you create data from a known 
distribution, then use your model to check that you can recover the known 
parameters from the data. Thus how the marks are created depends on what 
influences them. Here is a toy model to illustrate this - expanding on my code 
sample:


# a function to generate marks from parameters
rMarks <- function(n, m, s) {
  # a normal distribution limited to between 1 and 6, in 0.5 intervals, with
  # mean m and standard deviation s
  marks <- rnorm(n, m, s)
  marks <- round(marks * 2) / 2
  marks[marks < 1] <- 1
  marks[marks > 6] <- 6
  return(marks)
}

# Teachers in two categories: 70% of teachers (tNormal) grade everyone 
according to 
# a marks distribution with m = 3.5 and sd = 1 ; the others grade girls with a 
# m = 4.5 and sd = 0.7 and boys with m = 3.0 and sd = 1.2

# define who are the "normal teachers"
x <- paste0("t", 1:(nS * nTpS))
tNormal <- sample(x, round(nS * nTpS * 0.7), replace = FALSE)

# this is rather pedestrian code, but as explicit as I can make it ...
for (i in 1:nrow(mySim)) {
  if (mySim$Teacher[i] %in% tNormal) {
m <- 3.5
s <- 1.0
  } else {
if (mySim$Gender[i] == "girl") {
  m <- 4.5
  s <- 0.7
} else {
  m <- 3.0
  s <- 1.2 
}
  }
  mySim$Mark[i] <- rMarks(1, m, s)
}

# Validate
table(mySim$Mark)
hist(mySim$Mark[mySim$Teacher %in% tNormal],
 col = "#BB44")
hist(mySim$Mark[ ! mySim$Teacher %in% tNormal],
 add = TRUE,
 col = "#BB44")

Then the challenge is to recover the parameters from your analysis. 


Cheers,
Boris



> On 2019-05-19, at 11:14, varin sacha  wrote:
> 
> Dear Boris,
> 
> Great  But what about Mark in your R code ? Don't we have to precise in 
> the R code that mark ranges between 1 to 6 (1 ; 1.5 ; 2 ; 2.5 ; 3 ; 3.5 ; 4 ; 
> 4.5 ; 5 ; 5.5 ; 6) ?
> 
> By the way, to fit a linear mixed model, I use lme4 package and then the lmer 
> function works with the variables like in this example here below :
> 
> library(lme4)
> mm=lmer(Mark ~Gender + (1 | School / Class), data=Dataset) 
> 
> With your R code, how can I write the lmer function to make it work ?
> 
> Best,
> S.
> 
> 
> 
> 
> 
> 
> 
> Le dimanche 19 mai 2019 à 15:26:39 UTC+2, Boris Steipe 
>  a écrit : 
> 
> 
> 
> 
> 
> Fair enough - there are additional assumptions needed, which I make as 
> follows:
>   - each class has the same size
>   - each teacher teaches the same number of classes
>   - the number of boys and girls is random within a class
>   - there are 60% girls  (just for illustration that it does not have to be 
> equal)
>   
> 
> To make the dependencies explicit, I define them so, and in a way that they 
> can't be inconsistent.
> 
> nS <- 10# Schools
> nTpS <- 5  # Teachers per School
> nCpT <- 2  # Classes per teacher
> nPpC <- 20  # Pupils per class
> nS * nTpS * nCpT * nPpC == 2000  # Validate
> 
> 
> mySim <- data.frame(School  = paste0("s", rep(1:nS, each = nTpS*nCpT*nPpC)),
> Teacher = paste0("t", rep(1:(nTpS*nS), each = nCpT*nPpC)),
> Class  = paste0("c", rep(1:(nCpT*nTpS*nS), each = nPpC)),
> Gender  = sample(c("boy", "girl"),
> (nS*nTpS*nCpT*nPpC),
> prob = c(0.4, 0.6),
> replace = TRUE),
> Mark= numeric(nS*nTpS*nCpT*nPpC),
> stringsAsFactors = FALSE)
> 
> 
> Then you fill mySim$Mark with values from your linear mixed model ...
> 
> mySim$Mark[i] <- simMarks(mySim[i])  # ... or something equivalent.
> 
> 
> All good?
> 
> Cheers,
> Boris
> 
> 
> 
>> On 2019-05-19, at 08:05, varin sacha  wrote:
>> 
>> Many thanks to all of you for your responses.
>> 
>> So, I will try to be clearer with a larger example. Te end of my mail is the 
>> more important to understand what I am trying to do. I am trying to simulate 
>> data to fit a linear mixed model (nested not crossed). More precisely, I 
>> would love to get at the end of the process, a table (.txt) with columns and 
>> rows. Column 1 and Rows will be the 2000 pupils and the columns the 
>> different variables : Column 2 = classes ; Column 3 = teachers, Column 4 = 
>> schools ; Column 5 = gender (boy or girl) ; Column 6 = mark in Frecnh
>> 
>> Pupils are nested  in classes, classes are nested in schools. The teacher 
>> are part of the process.
>> 
>> I want to simulate a dataset with n=2000 pupils, 100 classes, 50 teachers 
>> and 10 schools.
>> - Pupils n°1 to pupils n°2000 (p1, p2, p3, p4, ..., p2000)
>> - Classes n°1 to classes n°100 (c1, c2, c3, c4,..., c100)
>> - Teachers n°1 to teacher n°50 ( t1, t2, t3, t4, ..., t50)
>> - Schools n°1 to chool n°10 (s1, s2, s3, s4, ..., s10)
>> 
>> The nested structure is as followed : 
>> 
>> -- School 1 with teacher 1 to teacher 5 (t1, t2, t3, t4 and t5) with classes 
>> 1 to classes 10 (c1, c2, c3, c4, c5, c6, c7, 

Re: [R] Nested structure data simulation

2019-05-19 Thread varin sacha via R-help
Dear Boris,

Great  But what about Mark in your R code ? Don't we have to precise in the 
R code that mark ranges between 1 to 6 (1 ; 1.5 ; 2 ; 2.5 ; 3 ; 3.5 ; 4 ; 4.5 ; 
5 ; 5.5 ; 6) ?

By the way, to fit a linear mixed model, I use lme4 package and then the lmer 
function works with the variables like in this example here below :

library(lme4)
mm=lmer(Mark ~Gender + (1 | School / Class), data=Dataset) 

With your R code, how can I write the lmer function to make it work ?

Best,
S.







Le dimanche 19 mai 2019 à 15:26:39 UTC+2, Boris Steipe 
 a écrit : 





Fair enough - there are additional assumptions needed, which I make as follows:
  - each class has the same size
  - each teacher teaches the same number of classes
  - the number of boys and girls is random within a class
  - there are 60% girls  (just for illustration that it does not have to be 
equal)
  

To make the dependencies explicit, I define them so, and in a way that they 
can't be inconsistent.

nS <- 10        # Schools
nTpS <- 5      # Teachers per School
nCpT <- 2      # Classes per teacher
nPpC <- 20      # Pupils per class
nS * nTpS * nCpT * nPpC == 2000  # Validate


mySim <- data.frame(School  = paste0("s", rep(1:nS, each = nTpS*nCpT*nPpC)),
                    Teacher = paste0("t", rep(1:(nTpS*nS), each = nCpT*nPpC)),
                    Class  = paste0("c", rep(1:(nCpT*nTpS*nS), each = nPpC)),
                    Gender  = sample(c("boy", "girl"),
                                    (nS*nTpS*nCpT*nPpC),
                                    prob = c(0.4, 0.6),
                                    replace = TRUE),
                    Mark    = numeric(nS*nTpS*nCpT*nPpC),
                    stringsAsFactors = FALSE)
                    

Then you fill mySim$Mark with values from your linear mixed model ...

mySim$Mark[i] <- simMarks(mySim[i])  # ... or something equivalent.


All good?

Cheers,
Boris



> On 2019-05-19, at 08:05, varin sacha  wrote:
> 
> Many thanks to all of you for your responses.
> 
> So, I will try to be clearer with a larger example. Te end of my mail is the 
> more important to understand what I am trying to do. I am trying to simulate 
> data to fit a linear mixed model (nested not crossed). More precisely, I 
> would love to get at the end of the process, a table (.txt) with columns and 
> rows. Column 1 and Rows will be the 2000 pupils and the columns the different 
> variables : Column 2 = classes ; Column 3 = teachers, Column 4 = schools ; 
> Column 5 = gender (boy or girl) ; Column 6 = mark in Frecnh
> 
> Pupils are nested  in classes, classes are nested in schools. The teacher are 
> part of the process.
> 
> I want to simulate a dataset with n=2000 pupils, 100 classes, 50 teachers and 
> 10 schools.
> - Pupils n°1 to pupils n°2000 (p1, p2, p3, p4, ..., p2000)
> - Classes n°1 to classes n°100 (c1, c2, c3, c4,..., c100)
> - Teachers n°1 to teacher n°50 ( t1, t2, t3, t4, ..., t50)
> - Schools n°1 to chool n°10 (s1, s2, s3, s4, ..., s10)
> 
> The nested structure is as followed : 
> 
> -- School 1 with teacher 1 to teacher 5 (t1, t2, t3, t4 and t5) with classes 
> 1 to classes 10 (c1, c2, c3, c4, c5, c6, c7, c8,c9,c10), pupils n°1 to pupils 
> n°200 (p1, p2, p3, p4,..., p200).
> 
> -- School 2 with teacher 6 to teacher 10, with classes 11 to classes 20, 
> pupils n°201 to pupils n°400
> 
> -- and so on
> 
> The table (.txt) I would love to get at the end is the following :
> 
>        Class    Teacher    School    gender    Mark
> 1      c1        t1                s1            boy        5
> 2      c1        t1                s1            boy        5.5
> 3      c1        t1                s1            girl        4.5
> 4      c1        t1                s1            girl        6
> 5      c1        t1                s1            boy      3.5
> 6      ...                                    .        .      
>         
> 
> The first 20 rows with c1, with t1, with s1, gender (randomly slected) and 
> mark (andomly selected) from 1 to 6
> The rows 21 to 40 with c2 with t1 with s1
> The rows 41 to 60 with c3 with t2 with s1
> The rows 61 to 80 with c4 with t2 with s1
> The rows 81 to 100 with c5 with t3 with s1
> The rows 101 to 120 with c6 with t3 with s1
> The rows 121 to 140 with c7 with t4 with s1
> The rows 141 to 160 with c8 with t4 with s1
> The rows 161 to 180 with c9 with t5 with s1
> The rows 181 to 200 with c10 with t5 with s1
> 
> The rows 201 to 220 with c11 with t6 with s2
> The rows 221 to 240 with c12 with t6 with s2
> 
> And so on...
> 
> Is it possible to do that ? Or am I dreaming ?
> 
> 
> Le dimanche 19 mai 2019 à 10:45:43 UTC+2, Linus Chen  
> a écrit : 
> 
> 
> 
> 
> 
> Dear varin sacha,
> 
> I think it will help us help you, if you give a clearer description of
> what exactly you want.
> 
> I assume the situation is that you know what a data structure you
> want, but do not know
> how to conveniently create such structure.
> And that is where others can 

Re: [R] Nested structure data simulation

2019-05-19 Thread Boris Steipe
Fair enough - there are additional assumptions needed, which I make as follows:
  - each class has the same size
  - each teacher teaches the same number of classes
  - the number of boys and girls is random within a class
  - there are 60% girls   (just for illustration that it does not have to be 
equal)
  

To make the dependencies explicit, I define them so, and in a way that they 
can't be inconsistent.

nS <- 10# Schools
nTpS <- 5   # Teachers per School
nCpT <- 2   # Classes per teacher
nPpC <- 20  # Pupils per class
nS * nTpS * nCpT * nPpC == 2000   # Validate


mySim <- data.frame(School  = paste0("s", rep(1:nS, each = nTpS*nCpT*nPpC)),
Teacher = paste0("t", rep(1:(nTpS*nS), each = nCpT*nPpC)),
Class   = paste0("c", rep(1:(nCpT*nTpS*nS), each = nPpC)),
Gender  = sample(c("boy", "girl"),
 (nS*nTpS*nCpT*nPpC),
 prob = c(0.4, 0.6),
 replace = TRUE),
Mark= numeric(nS*nTpS*nCpT*nPpC),
stringsAsFactors = FALSE)


Then you fill mySim$Mark with values from your linear mixed model ...

mySim$Mark[i] <- simMarks(mySim[i])  # ... or something equivalent.


All good?

Cheers,
Boris



> On 2019-05-19, at 08:05, varin sacha  wrote:
> 
> Many thanks to all of you for your responses.
> 
> So, I will try to be clearer with a larger example. Te end of my mail is the 
> more important to understand what I am trying to do. I am trying to simulate 
> data to fit a linear mixed model (nested not crossed). More precisely, I 
> would love to get at the end of the process, a table (.txt) with columns and 
> rows. Column 1 and Rows will be the 2000 pupils and the columns the different 
> variables : Column 2 = classes ; Column 3 = teachers, Column 4 = schools ; 
> Column 5 = gender (boy or girl) ; Column 6 = mark in Frecnh
> 
> Pupils are nested  in classes, classes are nested in schools. The teacher are 
> part of the process.
> 
> I want to simulate a dataset with n=2000 pupils, 100 classes, 50 teachers and 
> 10 schools.
> - Pupils n°1 to pupils n°2000 (p1, p2, p3, p4, ..., p2000)
> - Classes n°1 to classes n°100 (c1, c2, c3, c4,..., c100)
> - Teachers n°1 to teacher n°50 ( t1, t2, t3, t4, ..., t50)
> - Schools n°1 to chool n°10 (s1, s2, s3, s4, ..., s10)
> 
> The nested structure is as followed : 
> 
> -- School 1 with teacher 1 to teacher 5 (t1, t2, t3, t4 and t5) with classes 
> 1 to classes 10 (c1, c2, c3, c4, c5, c6, c7, c8,c9,c10), pupils n°1 to pupils 
> n°200 (p1, p2, p3, p4,..., p200).
> 
> -- School 2 with teacher 6 to teacher 10, with classes 11 to classes 20, 
> pupils n°201 to pupils n°400
> 
> -- and so on
> 
> The table (.txt) I would love to get at the end is the following :
> 
> ClassTeacherSchoolgenderMark
> 1   c1t1s1boy5
> 2   c1t1s1boy5.5
> 3   c1t1s1girl4.5
> 4   c1t1s1girl6
> 5   c1t1s1boy   3.5
> 6   ..... 
>   
> 
> The first 20 rows with c1, with t1, with s1, gender (randomly slected) and 
> mark (andomly selected) from 1 to 6
> The rows 21 to 40 with c2 with t1 with s1
> The rows 41 to 60 with c3 with t2 with s1
> The rows 61 to 80 with c4 with t2 with s1
> The rows 81 to 100 with c5 with t3 with s1
> The rows 101 to 120 with c6 with t3 with s1
> The rows 121 to 140 with c7 with t4 with s1
> The rows 141 to 160 with c8 with t4 with s1
> The rows 161 to 180 with c9 with t5 with s1
> The rows 181 to 200 with c10 with t5 with s1
> 
> The rows 201 to 220 with c11 with t6 with s2
> The rows 221 to 240 with c12 with t6 with s2
> 
> And so on...
> 
> Is it possible to do that ? Or am I dreaming ?
> 
> 
> Le dimanche 19 mai 2019 à 10:45:43 UTC+2, Linus Chen  
> a écrit : 
> 
> 
> 
> 
> 
> Dear varin sacha,
> 
> I think it will help us help you, if you give a clearer description of
> what exactly you want.
> 
> I assume the situation is that you know what a data structure you
> want, but do not know
> how to conveniently create such structure.
> And that is where others can help you.
> So, please, describe the wanted data structure more thoroughly,
> ideally with example.
> 
> Thanks,
> Lei
> 
> On Sat, May 18, 2019 at 10:04 PM varin sacha via R-help
>  wrote:
>> 
>> Dear Boris,
>> 
>> Yes, top-down, no problem. Many thanks, but in your code did you not forget 
>> "teacher" ? As a reminder teacher has to be nested with classes. I mean the 
>> 50 pupils belonging to C1 must be with (teacher 1) T1, the 50 pupils 
>> belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so on.
>> 
>> Best,
>> 
>> 
>> Le samedi 18 

Re: [R] Nested structure data simulation

2019-05-19 Thread varin sacha via R-help
Many thanks to all of you for your responses.

So, I will try to be clearer with a larger example. Te end of my mail is the 
more important to understand what I am trying to do. I am trying to simulate 
data to fit a linear mixed model (nested not crossed). More precisely, I would 
love to get at the end of the process, a table (.txt) with columns and rows. 
Column 1 and Rows will be the 2000 pupils and the columns the different 
variables : Column 2 = classes ; Column 3 = teachers, Column 4 = schools ; 
Column 5 = gender (boy or girl) ; Column 6 = mark in Frecnh

Pupils are nested  in classes, classes are nested in schools. The teacher are 
part of the process.

I want to simulate a dataset with n=2000 pupils, 100 classes, 50 teachers and 
10 schools.
- Pupils n°1 to pupils n°2000 (p1, p2, p3, p4, ..., p2000)
- Classes n°1 to classes n°100 (c1, c2, c3, c4,..., c100)
- Teachers n°1 to teacher n°50 ( t1, t2, t3, t4, ..., t50)
- Schools n°1 to chool n°10 (s1, s2, s3, s4, ..., s10)

The nested structure is as followed : 

-- School 1 with teacher 1 to teacher 5 (t1, t2, t3, t4 and t5) with classes 1 
to classes 10 (c1, c2, c3, c4, c5, c6, c7, c8,c9,c10), pupils n°1 to pupils 
n°200 (p1, p2, p3, p4,..., p200).

-- School 2 with teacher 6 to teacher 10, with classes 11 to classes 20, pupils 
n°201 to pupils n°400

-- and so on

The table (.txt) I would love to get at the end is the following :

    ClassTeacher    School    gender    Mark
1   c1    t1    s1    boy    5
2   c1t1s1boy5.5
3   c1t1s1girl4.5
4   c1t1s1girl6
5   c1t1s1boy   3.5
6   ...            .    .   
    

The first 20 rows with c1, with t1, with s1, gender (randomly slected) and mark 
(andomly selected) from 1 to 6
The rows 21 to 40 with c2 with t1 with s1
The rows 41 to 60 with c3 with t2 with s1
The rows 61 to 80 with c4 with t2 with s1
The rows 81 to 100 with c5 with t3 with s1
The rows 101 to 120 with c6 with t3 with s1
The rows 121 to 140 with c7 with t4 with s1
The rows 141 to 160 with c8 with t4 with s1
The rows 161 to 180 with c9 with t5 with s1
The rows 181 to 200 with c10 with t5 with s1

The rows 201 to 220 with c11 with t6 with s2
The rows 221 to 240 with c12 with t6 with s2

And so on...

Is it possible to do that ? Or am I dreaming ?


Le dimanche 19 mai 2019 à 10:45:43 UTC+2, Linus Chen  a 
écrit : 





Dear varin sacha,

I think it will help us help you, if you give a clearer description of
what exactly you want.

I assume the situation is that you know what a data structure you
want, but do not know
how to conveniently create such structure.
And that is where others can help you.
So, please, describe the wanted data structure more thoroughly,
ideally with example.

Thanks,
Lei

On Sat, May 18, 2019 at 10:04 PM varin sacha via R-help
 wrote:
>
> Dear Boris,
>
> Yes, top-down, no problem. Many thanks, but in your code did you not forget 
> "teacher" ? As a reminder teacher has to be nested with classes. I mean the 
> 50 pupils belonging to C1 must be with (teacher 1) T1, the 50 pupils 
> belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so on.
>
> Best,
>
>
> Le samedi 18 mai 2019 à 16:52:48 UTC+2, Boris Steipe 
>  a écrit :
>
>
>
>
>
> Can you build your data top-down?
>
>
>
> schools <- paste("s", 1:6, sep="")
>
> classes <- character()
> for (school in schools) {
>  classes <- c(classes, paste(school, paste("c", 1:5, sep=""), sep = "."))
> }
>
> pupils <- character()
> for (class in classes) {
>  pupils <- c(pupils, paste(class, paste("p", 1:10, sep=""), sep = "."))
> }
>
>
>
> B.
>
>
>
> > On 2019-05-18, at 09:57, varin sacha via R-help  
> > wrote:
> >
> > Dear R-Experts,
> >
> > In a data simulation, I would like a balanced distribution with a nested 
> > structure for classroom and teacher (not for school). I mean 50 pupils 
> > belonging to C1, 50 other pupils belonging to C2, 50 other pupils belonging 
> > to C3 and so on. Then I want the 50 pupils belonging to C1 with T1, the 50 
> > pupils belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and 
> > so on. The school don’t have to be nested, I just want a balanced 
> > distribution, I mean 60 pupils in S1, 60 other pupils in S2 and so on.
> > Here below the reproducible example.
> > Many thanks for your help.
> >
> > ##
> > set.seed(123)
> > # Génération aléatoire des colonnes
> > pupils<-1:300
> > classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)  
> > teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)  
> > school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T)
>
> > ##
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 

Re: [R] Nested structure data simulation

2019-05-18 Thread Daniel Nordlund



On 5/18/2019 1:03 PM, varin sacha via R-help wrote:

Dear Boris,

Yes, top-down, no problem. Many thanks, but in your code did you not forget 
"teacher" ? As a reminder teacher has to be nested with classes. I mean the 50 
pupils belonging to C1 must be with (teacher 1) T1, the 50 pupils belonging to C2 with 
T2, the 50 pupils belonging to C3 with T3 and so on.

Best,


Le samedi 18 mai 2019 à 16:52:48 UTC+2, Boris Steipe  
a écrit :




<<>>

Given your design, you cannot distinguish between class and teacher. 
They are one and the same thing.  It doesn't make sense to include both 
in your model.


Dan

--
Daniel Nordlund
Port Townsend, WA  USA

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested structure data simulation

2019-05-18 Thread varin sacha via R-help
Dear Boris,

Yes, top-down, no problem. Many thanks, but in your code did you not forget 
"teacher" ? As a reminder teacher has to be nested with classes. I mean the 50 
pupils belonging to C1 must be with (teacher 1) T1, the 50 pupils belonging to 
C2 with T2, the 50 pupils belonging to C3 with T3 and so on.

Best,


Le samedi 18 mai 2019 à 16:52:48 UTC+2, Boris Steipe  
a écrit : 





Can you build your data top-down?



schools <- paste("s", 1:6, sep="")

classes <- character()
for (school in schools) {
  classes <- c(classes, paste(school, paste("c", 1:5, sep=""), sep = "."))
}

pupils <- character()
for (class in classes) {
  pupils <- c(pupils, paste(class, paste("p", 1:10, sep=""), sep = "."))
}



B.



> On 2019-05-18, at 09:57, varin sacha via R-help  wrote:
> 
> Dear R-Experts,
> 
> In a data simulation, I would like a balanced distribution with a nested 
> structure for classroom and teacher (not for school). I mean 50 pupils 
> belonging to C1, 50 other pupils belonging to C2, 50 other pupils belonging 
> to C3 and so on. Then I want the 50 pupils belonging to C1 with T1, the 50 
> pupils belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so 
> on. The school don’t have to be nested, I just want a balanced distribution, 
> I mean 60 pupils in S1, 60 other pupils in S2 and so on. 
> Here below the reproducible example. 
> Many thanks for your help.
> 
> ##
> set.seed(123)  
> # Génération aléatoire des colonnes 
> pupils<-1:300  
> classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)  
> teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)  
> school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T)    

> ##
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested structure data simulation

2019-05-18 Thread Boris Steipe
Can you build your data top-down?



schools <- paste("s", 1:6, sep="")

classes <- character()
for (school in schools) {
  classes <- c(classes, paste(school, paste("c", 1:5, sep=""), sep = "."))
}

pupils <- character()
for (class in classes) {
  pupils <- c(pupils, paste(class, paste("p", 1:10, sep=""), sep = "."))
}



B.



> On 2019-05-18, at 09:57, varin sacha via R-help  wrote:
> 
> Dear R-Experts,
> 
> In a data simulation, I would like a balanced distribution with a nested 
> structure for classroom and teacher (not for school). I mean 50 pupils 
> belonging to C1, 50 other pupils belonging to C2, 50 other pupils belonging 
> to C3 and so on. Then I want the 50 pupils belonging to C1 with T1, the 50 
> pupils belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so 
> on. The school don’t have to be nested, I just want a balanced distribution, 
> I mean 60 pupils in S1, 60 other pupils in S2 and so on. 
> Here below the reproducible example. 
> Many thanks for your help.
> 
> ##
> set.seed(123)   
> # Génération aléatoire des colonnes 
> pupils<-1:300   
> classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)   
> teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)   
> school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T) 
> ##
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested structure data simulation

2019-05-18 Thread varin sacha via R-help
Many thanks Jeff and Linus,

Yes to Jeff,

OK with Linus but

classroom <- rep(c("C1","C2","C3","C4","C5","C6"), 50) [sample(1:300)] 

how can I include the nested structure, I mean the teacher. Now, I would like 
the 50 pupils belonging to C1 with T1, the 50 pupils belonging to C2 with T2, 
the 50 pupils belonging to C3 with T3 and so on.






Le samedi 18 mai 2019 à 16:20:33 UTC+2, Linus Chen  a 
écrit : 





Dear varin sacha,

Not very sure what you want, but will the following help a little?

tmp <- rep(c("C1","C2","C3","C4","C5","C6"), 50) # make a character
vector, with 50 "C1", 50 "C2", ...
classroom <- tmp[sample(1:300)] # make a random permutation.

Certainly you may also make it into one line:
classroom <- rep(c("C1","C2","C3","C4","C5","C6"), 50) [sample(1:30)]

Best,
Lei Chen

On Sat, May 18, 2019 at 3:57 PM varin sacha via R-help
 wrote:
>
> Dear R-Experts,
>
> In a data simulation, I would like a balanced distribution with a nested 
> structure for classroom and teacher (not for school). I mean 50 pupils 
> belonging to C1, 50 other pupils belonging to C2, 50 other pupils belonging 
> to C3 and so on. Then I want the 50 pupils belonging to C1 with T1, the 50 
> pupils belonging to C2 with T2, the 50 pupils belonging to C3 with T3 and so 
> on. The school don’t have to be nested, I just want a balanced distribution, 
> I mean 60 pupils in S1, 60 other pupils in S2 and so on.
> Here below the reproducible example.
> Many thanks for your help.
>
> ##
> set.seed(123)
> # Génération aléatoire des colonnes
> pupils<-1:300
> classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)  
> teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)  
> school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T)

> ##
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested structure data simulation

2019-05-18 Thread Jeff Newmiller
Wouldn't the students/teachers/schools be enumerated and the properties you are 
studying be random/correlated according to the enumerated values?

On May 18, 2019 6:57:06 AM PDT, varin sacha via R-help  
wrote:
>Dear R-Experts,
>
>In a data simulation, I would like a balanced distribution with a
>nested structure for classroom and teacher (not for school). I mean 50
>pupils belonging to C1, 50 other pupils belonging to C2, 50 other
>pupils belonging to C3 and so on. Then I want the 50 pupils belonging
>to C1 with T1, the 50 pupils belonging to C2 with T2, the 50 pupils
>belonging to C3 with T3 and so on. The school don’t have to be nested,
>I just want a balanced distribution, I mean 60 pupils in S1, 60 other
>pupils in S2 and so on. 
>Here below the reproducible example. 
>Many thanks for your help.
>
>##
>set.seed(123)   
># Génération aléatoire des colonnes 
>pupils<-1:300   
>classroom<-sample(c("C1","C2","C3","C4","C5","C6"),300,replace=T)  
>teacher<-sample(c("T1","T2","T3","T4","T5","T6"),300,replace=T)  
>school<-sample(c("S1","S2","S3","S4","S5"),300,replace=T)     
>##
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested mixed effectts question

2019-01-18 Thread Caroline
Great! Your suggestions made perfect sense and worked well. Thank you so much. 

> On Jan 18, 2019, at 3:33 AM, Phillip Alday  wrote:
> 
> (once again with the list)
> 
> Hi Caroline,
> 
> This question is probably better suited to r-sig-mixed-models
> (https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models). Some things
> are hard to tell without better understanding your design (I am not an
> ecologist/relevant type of biologist), but I'll give it a go.
> 
> I suspect that your model is over-parameterized. It's very rare to see a
> factor occur both as a fixed effect and as a grouping variable (the
> stuff behind the | ) in the random effects.
> 
> If you don't care about particular sites but rather only the general
> pattern across sites, then I would start with the model:
> 
> wrack.biomass ~ year  + (1 + year | site/trans)
> 
> This treats site as a known source of variance, but not one that you
> care about estimating particular effects for. You can still extract
> predictions for them, i.e. the BLUPs, via coef(wrackbio), but their
> theoretical interpretation is a bit different than the other option below.
> 
> If you do care about particular sites, I would use the model
> 
> # if your transects are uniquely labeled across sites
> wrack.biomass ~ year * site + (1 | trans)
> # if the transect labels are only unique within sites
> wrack.biomass ~ year * site + (1 | sites:trans)
> 
> This will give you fixed effects as in your model, but models the
> transects as a source of repetition and hence variance due to that
> grouping. The choice of exact specification depends on the labeling in
> your dataset; the sites:trans just guarantees unique labelling. The
> random effect in this case would estimate the average variance across
> all sites due to transects.
> 
> Best,
> Phillip
> 
> 
> 
> 
> On 16/01/19 12:00, r-help-requ...@r-project.org wrote:
>> Send R-help mailing list submissions to
> 
>> Today's Topics:
>> 
>>   6. Nested mixed effectts question (Caroline)
>> --
>> Hi,
>> 
>> I am helping a friend with an analysis for a study where she sampled
> wrack biomass in 15 different sites across three years. At each site,
> she sampled from three different transects. She is trying to estimate
> the effect of year*site on biomass while accounting for the nested
> nature (site/transcet) and repeated measure study design.
>> 
>> wrack.biomass ~ year * site + (1 | site/trans)
>> 
>> However she gets the following warning messages:
>> Warning messages:
>> 1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,  :
>>  unable to evaluate scaled gradient
>> 2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,  :
>>   Hessian is numerically singular: parameters are not uniquely determined
>> 
>> And her model output is:
>> 
>>> summary(wrackbio)
>> Linear mixed model fit by REML
>> t-tests use  Satterthwaite approximations to degrees of freedom
> ['lmerMod']
>> Formula: (actual.mean.biomass.m2.50.m.transect) ~ year * site + (1 |
> site/trans)
>>   Data: wrack_resp_allyrs_transname
>> 
>> REML criterion at convergence: 691
>> 
>> Scaled residuals:
>>Min  1Q  Median  3Q Max
>> -3.3292 -0.2624 -0.0270  0.1681  3.8024
>> 
>> Random effects:
>> Groups NameVariance Std.Dev.
>> trans:site (Intercept)  0.  0.
>> site   (Intercept)  0.5531  0.7437
>> Residual   94.6453  9.7286
>> Number of obs: 132, groups:  trans:site, 44; site, 15
>> 
>> Fixed effects:
>>Estimate Std. Error df t value Pr(>|t|)
>> (Intercept)9.692e+00  5.666e+00  1.119e-04   1.7110.999
>> year2016   1.256e+01  7.943e+00  8.700e+01   1.5820.117
>> year2017   2.395e+00  7.943e+00  8.700e+01   0.3020.764
>> siteCL 5.672e+01  8.013e+00  1.119e-04   7.0790.999
>> siteDO-4.315e+00  8.013e+00  1.119e-04  -0.5390.999
>> siteFL 7.872e+00  8.013e+00  1.119e-04   0.9820.999
>> siteFS-7.619e+00  8.013e+00  1.119e-04  -0.9510.999
>> siteGH 4.369e+00  8.013e+00  1.119e-04   0.5450.999
>> siteLB-3.747e+00  8.013e+00  1.119e-04  -0.4680.999
>> siteLBP   -5.298e+00  8.943e+00  1.736e-04  -0.5920.999
>> siteNB-2.953e+00  8.013e+00  1.119e-04  -0.3691.000
>> siteNS 1.005e+00  8.013e+00  1.119e-04   0.1251.000
>> sitePC-5.238e+00  8.013e+00  1.119e-04  -0.6540.999
>> siteSB-7.649e+00  8.013e+00  1.119e-04  -0.9550.999
>> siteSILT  -4.734e+00  8.013e+00  1.119e-04  -0.5910.999
>> siteSL-7.890e+00  8.013e+00  1.119e-04  -0.9850.999
>> siteUD-8.230e+00  8.013e+00  1.119e-04  -1.0270.999
>> year2016:siteCL   -6.359e+01  1.123e+01  8.700e+01  -5.660 1.91e-07 ***
>> year2017:siteCL   -5.210e+01  1.123e+01  8.700e+01  -4.638 1.23e-05 ***
>> 

Re: [R] Nested mixed effectts question

2019-01-18 Thread Phillip Alday
(once again with the list)

Hi Caroline,

This question is probably better suited to r-sig-mixed-models
(https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models). Some things
are hard to tell without better understanding your design (I am not an
ecologist/relevant type of biologist), but I'll give it a go.

I suspect that your model is over-parameterized. It's very rare to see a
factor occur both as a fixed effect and as a grouping variable (the
stuff behind the | ) in the random effects.

If you don't care about particular sites but rather only the general
pattern across sites, then I would start with the model:

wrack.biomass ~ year  + (1 + year | site/trans)

This treats site as a known source of variance, but not one that you
care about estimating particular effects for. You can still extract
predictions for them, i.e. the BLUPs, via coef(wrackbio), but their
theoretical interpretation is a bit different than the other option below.

If you do care about particular sites, I would use the model

# if your transects are uniquely labeled across sites
wrack.biomass ~ year * site + (1 | trans)
# if the transect labels are only unique within sites
wrack.biomass ~ year * site + (1 | sites:trans)

This will give you fixed effects as in your model, but models the
transects as a source of repetition and hence variance due to that
grouping. The choice of exact specification depends on the labeling in
your dataset; the sites:trans just guarantees unique labelling. The
random effect in this case would estimate the average variance across
all sites due to transects.

Best,
Phillip




On 16/01/19 12:00, r-help-requ...@r-project.org wrote:
> Send R-help mailing list submissions to

> Today's Topics:
>
>6. Nested mixed effectts question (Caroline)
> --
> Hi,
>
> I am helping a friend with an analysis for a study where she sampled
wrack biomass in 15 different sites across three years. At each site,
she sampled from three different transects. She is trying to estimate
the effect of year*site on biomass while accounting for the nested
nature (site/transcet) and repeated measure study design.
>
> wrack.biomass ~ year * site + (1 | site/trans)
>
> However she gets the following warning messages:
> Warning messages:
> 1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,  :
>   unable to evaluate scaled gradient
> 2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,  :
>Hessian is numerically singular: parameters are not uniquely determined
>
> And her model output is:
>
>> summary(wrackbio)
> Linear mixed model fit by REML
> t-tests use  Satterthwaite approximations to degrees of freedom
['lmerMod']
> Formula: (actual.mean.biomass.m2.50.m.transect) ~ year * site + (1 |
site/trans)
>Data: wrack_resp_allyrs_transname
>
> REML criterion at convergence: 691
>
> Scaled residuals:
> Min  1Q  Median  3Q Max
> -3.3292 -0.2624 -0.0270  0.1681  3.8024
>
> Random effects:
>  Groups NameVariance Std.Dev.
>  trans:site (Intercept)  0.  0.
>  site   (Intercept)  0.5531  0.7437
>  Residual   94.6453  9.7286
> Number of obs: 132, groups:  trans:site, 44; site, 15
>
> Fixed effects:
> Estimate Std. Error df t value Pr(>|t|)
> (Intercept)9.692e+00  5.666e+00  1.119e-04   1.7110.999
> year2016   1.256e+01  7.943e+00  8.700e+01   1.5820.117
> year2017   2.395e+00  7.943e+00  8.700e+01   0.3020.764
> siteCL 5.672e+01  8.013e+00  1.119e-04   7.0790.999
> siteDO-4.315e+00  8.013e+00  1.119e-04  -0.5390.999
> siteFL 7.872e+00  8.013e+00  1.119e-04   0.9820.999
> siteFS-7.619e+00  8.013e+00  1.119e-04  -0.9510.999
> siteGH 4.369e+00  8.013e+00  1.119e-04   0.5450.999
> siteLB-3.747e+00  8.013e+00  1.119e-04  -0.4680.999
> siteLBP   -5.298e+00  8.943e+00  1.736e-04  -0.5920.999
> siteNB-2.953e+00  8.013e+00  1.119e-04  -0.3691.000
> siteNS 1.005e+00  8.013e+00  1.119e-04   0.1251.000
> sitePC-5.238e+00  8.013e+00  1.119e-04  -0.6540.999
> siteSB-7.649e+00  8.013e+00  1.119e-04  -0.9550.999
> siteSILT  -4.734e+00  8.013e+00  1.119e-04  -0.5910.999
> siteSL-7.890e+00  8.013e+00  1.119e-04  -0.9850.999
> siteUD-8.230e+00  8.013e+00  1.119e-04  -1.0270.999
> year2016:siteCL   -6.359e+01  1.123e+01  8.700e+01  -5.660 1.91e-07 ***
> year2017:siteCL   -5.210e+01  1.123e+01  8.700e+01  -4.638 1.23e-05 ***
> year2016:siteDO   -1.550e+01  1.123e+01  8.700e+01  -1.3800.171
> year2017:siteDO   -3.022e+00  1.123e+01  8.700e+01  -0.2690.789
> year2016:siteFL   -7.522e+00  1.123e+01  8.700e+01  -0.6700.505
> year2017:siteFL   -1.167e+01  1.123e+01  8.700e+01  -1.0390.302
> year2016:siteFS   -1.391e+01  

Re: [R] nested loop

2017-09-09 Thread Ek Esawi
I would try fininterval as well. It should do what you have asked provided
that you take care of the issue Ulrik pointed out.
Best of luck--EK

On Fri, Sep 8, 2017 at 6:15 AM, Hemant Sain  wrote:

> i have a vector containing values ranging from 0 to 24
> i want to create another variable which can categorize those values  like
> this
> please help me with an R code
>
> Thanks
>
> *Value   New_Var*10 -5
> 30 -5
> 50 -5
> 96-10
> 76-10
> 56-10
> 4 0-5
> 11  11-15
> 12 11-15
> 18  16-20
> 23  21 -25
>
> --
> hemantsain.com
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested loop R code

2017-09-08 Thread Ulrik Stervbo
Hi Hemant,

please write to the r-help list in the future.

Look at the cut () function to solve your problem.

Also, you have a problem in your example - 5 is placed in two different
categories.

HTH
Ulrik

On Fri, 8 Sep 2017 at 12:16 Hemant Sain  wrote:

> i have a vector containing values ranging from 0 to 24
> i want to create another variable which can categorize those values  like
> this
> please help me with an R code
>
> Thanks
>
> *Value   New_Var*10 -5
> 30 -5
> 50 -5
> 96-10
> 76-10
> 56-10
> 4 0-5
> 11  11-15
> 12 11-15
> 18  16-20
> 23  21 -25
>
>
> --
> hemantsain.com
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested for loop

2017-08-08 Thread S Ellison
> The code I've attached works for a population of 400 and samples 100 times.
> I'd like to extend this to 300 samples and 3 populations. So, the x-axis would
> range from 0-300 samples.
> 
> What I'm having trouble with is finding a way to change the population mid-
> way through the function. I want samples 1-100 to be taken from a
> population of 400, samples 101-200 to be taken from a sample of 800 and
> samples 201-300 from a population of 300. The end result should look
> something like a heart rate monitor.

You could write your function to take a list of either subpopulations or sets 
of population parameters, lapply your simulation generator over the list and 
(assuming the output from each of those is a vector) use c(that.list, 
recursive=TRUE) to concatenate the resulting list of vectors into a single 
vector.


S Ellison


***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested cross validation with lapply

2017-08-08 Thread Suzen, Mehmet
Hi Jesús,

Do you have a code you tried without lapply? Why don't you post that here too?

There are a couple of packages supporting nested CV; TANDEM, blkbox
you may want to check their code.

Also, `cvTools` package may help you to write one.

On 7 August 2017 at 15:21, Jesús Para Fernández
 wrote:
> Hi all!!
>
> How can i do nested cross validation with lapply??
>
> I know caret package, but I want to do it manuallly using lapply instead for 
> bucle.
>
> Thanks!!
> Jesús
>
> [[alternative HTML version deleted]]
>
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] Nested for loop

2017-08-07 Thread Kirsten Morehouse
Hi Caitlin and Ben,

Thanks for your responses! My issue is that I'd like to create one
continuous line, rather than 3 lines overlayed.

The code I've attached works for a population of 400 and samples 100 times.
I'd like to extend this to 300 samples and 3 populations. So, the x-axis
would range from 0-300 samples.

What I'm having trouble with is finding a way to change the population
mid-way through the function. I want samples 1-100 to be taken from a
population of 400, samples 101-200 to be taken from a sample of 800 and
samples 201-300 from a population of 300. The end result should look
something like a heart rate monitor.

Aside from the rationale, does what I'm explaining make sense?

Best,

Kirsten

On Mon, Aug 7, 2017 at 3:18 PM, Caitlin  wrote:

> Hi.
>
> A nested for loop is not terribly efficient (it's O(n^2)). Can you
> vectorize it? If so, this would be a far more efficient and faster approach.
>
> ~Caitlin
>
> On Saturday, August 5, 2017, Kirsten Morehouse 
> wrote:
>
>> Hi! Thanks for taking the time to read this.
>>
>> The code below creates a graph that takes 100 samples that are between 5%
>> and 15% of the population (400).
>>
>> What I'd like to do, however, is add two other sections to the graph. It
>> would look something like this:
>>
>> from 1-100 samples take 100 samples that are between 5% and 15% of the
>> population (400). From 101-200 take 100 samples that are between 5% and
>> 15%
>> of the population (800). From 201-300 take 100 samples that are between 5%
>> and 15% of the population (300).
>>
>> I assume this would require a nested for loop. Does anyone have advice as
>> to how to do this?
>>
>> Thanks for your time. Kirsten
>>
>> ## Mark-Recapture
>> ## Estimate popoulation from repeated sampling
>>
>> ## Population size
>> N <- 400
>> N
>>
>> ## Vector labeling each item in the population
>> pop <- c(1:N)
>> pop
>>
>> ## Lower and upper bounds of sample size
>> lower.bound <- round(x = .05 * N, digits = 0)
>> lower.bound ## Smallest possible sample size
>>
>> upper.bound <- round(x = .15 * N, digits = 0)
>> upper.bound ## Largest possible sample size
>>
>> ## Length of sample size interval
>> length.ss.interval <- length(c(lower.bound:upper.bound))
>> length.ss.interval ## total possible sample sizes, ranging form
>> lower.bound
>> to upper.bound
>>
>> ## Determine a sample size randomly (not a global variable...simply for
>> test purposes)
>> ## Between lower and upper bounds set previously
>> ## Give equal weight to each possible sample size in this interval
>> sample(x = c(lower.bound:upper.bound),
>>size = 1,
>>prob = c(rep(1/length.ss.interval, length.ss.interval)))
>>
>> ## Specify number of samples to take
>> n.samples <- 100
>>
>> ## Initiate empty matrix
>> ## 1st column is population (item 1 thorugh item 400)
>> ## 2nd through nth column are all rounds of sampling
>> dat <- matrix(data = NA,
>>   nrow = length(pop),
>>   ncol = n.samples + 1)
>>
>> dat[,1] <- pop
>>
>> dat
>>
>> ## Take samples of random sizes
>> ## Record results in columns 2 through n
>> ## 1 = sampled (marked)
>> ## 0 = not sampled (not marked)
>> for(i in 2:ncol(dat)) {
>>   a.sample <- sample(x = pop,
>>  size = sample(x = c(lower.bound:upper.bound),
>>size = 1,
>>prob = c(rep(1/length.ss.interval,
>> length.ss.interval))),
>>  replace = FALSE)
>>   dat[,i] <- dat[,1] %in% a.sample
>> }
>>
>> ## How large was each sample size?
>> apply(X = dat, MARGIN = 2, FUN = sum)
>> ## 1st element is irrelevant
>> ## 2nd element through nth element: sample size for each of the 100
>> samples
>>
>> ## At this point, all computations can be done using dat
>>
>> ## Create Schnabel dataframe using dat
>> ## Google the Schnabel formula
>>
>> schnabel.comp <- data.frame(sample = 1:n.samples,
>> n.sampled = apply(X = dat, MARGIN = 2, FUN =
>> sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
>> )
>>
>> ## First column: which sample, 1-100
>> ## Second column: number selected in that sample
>>
>>
>> ## How many items were previously sampled?
>> ## For 1st sample, it's 0
>> ## For 2nd sample, code is different than for remaning samples
>>
>> n.prev.sampled <- c(0, rep(NA, n.samples-1))
>> n.prev.sampled
>>
>> n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
>> yes = 1,
>> no = 0))
>>
>> n.prev.sampled
>>
>> for(i in 4:ncol(dat)) {
>>   n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
>> rowSums(dat[,2:(i-1)]) > 0,
>> yes = 1,
>> no = 0))
>> }
>>
>> schnabel.comp$n.prev.sampled <- n.prev.sampled
>>
>> ## n.newly.sampled: in each sample, how many items were newly sampled?
>> ## i.e., never seen before?
>> 

Re: [R] Nested cross validation with lapply

2017-08-07 Thread Bert Gunter
This is an inappropriate question for this list. See the posting guide
below and list archives for what is appropriate.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Aug 7, 2017 at 6:21 AM, Jesús Para Fernández
 wrote:
> Hi all!!
>
> How can i do nested cross validation with lapply??
>
> I know caret package, but I want to do it manuallly using lapply instead for 
> bucle.
>
> Thanks!!
> Jesús
>
> [[alternative HTML version deleted]]
>
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] Nested for loop

2017-08-07 Thread Ben Tupper
Hmmm.

If I understand you correctly, your question has to do with adding lines to 
your graph?  If so, my ggplot2 skills are sort of floppy, but you could append 
your sampling results to your data frame (one for each sample set) and then 
simply add layers.  Sort of like this.

N <- 10
x <- 1:N
df <- data.frame(
x = x,
y1 = sample(x, N, replace = TRUE),
y2 = sample(x, N, replace = TRUE),
y3 = sample(x, N, replace = TRUE))

ggplot(df, mapping = aes(x = x, y = y1)) +
geom_point(aes(y = y1), col = 'orange') + geom_line(aes(y = y1),col = 
'orange') +
geom_point(aes(y = y2), col = 'blue') + geom_line(aes(y = y2), col = 
'blue') +
geom_point(aes(y = y3), col = 'gray') + geom_line(aes(y = y3), col = 
'gray')

If plotting is not the issue then I don't understand what your question is.

Cheers,
Ben


> On Aug 6, 2017, at 3:44 PM, Kirsten Morehouse  wrote:
> 
> Hi Ben,
> 
> That's exactly right! Except for each set it's the sample population that is 
> 400, 800 or 300. I want to take 3 samples, each of 100, where only the 
> population differs. I can do this separately, but I'm having trouble putting 
> them all on the same graph. 
> 
> I'd like to have sample on the x axis (1-300) and estimate on the y axis. I 
> want to show how population affects the estimates. 
> 
> Does this make more sense?
> 
> Thanks for your time!
> 
> Kirsten 
> On Sun, Aug 6, 2017 at 3:21 PM Ben Tupper  > wrote:
> Hi Kirsten,
> 
> 
> 
> I can run your example code but I can't quite follow your division of 
> sampling.  Can you restate the the task?  Below is what I think you are 
> asking for, but I have the feeling I may be off the mark.
> 
> 
> 
> 
> 
> Set A: 400 samples, draw 100 in range of 5 to 15
> 
> 
> 
> Set B: 800 samples, draw 100 in range of 5 to 15
> 
> 
> 
> Set C: 300 samples, draw 100 in range of 5 to 15
> 
> 
> 
> Ben
> 
> 
> 
> > On Aug 5, 2017, at 9:21 AM, Kirsten Morehouse  > > wrote:
> 
> >
> 
> > Hi! Thanks for taking the time to read this.
> 
> >
> 
> > The code below creates a graph that takes 100 samples that are between 5%
> 
> > and 15% of the population (400).
> 
> >
> 
> > What I'd like to do, however, is add two other sections to the graph. It
> 
> > would look something like this:
> 
> >
> 
> > from 1-100 samples take 100 samples that are between 5% and 15% of the
> 
> > population (400). From 101-200 take 100 samples that are between 5% and 15%
> 
> > of the population (800). From 201-300 take 100 samples that are between 5%
> 
> > and 15% of the population (300).
> 
> >
> 
> > I assume this would require a nested for loop. Does anyone have advice as
> 
> > to how to do this?
> 
> >
> 
> > Thanks for your time. Kirsten
> 
> >
> 
> > ## Mark-Recapture
> 
> > ## Estimate popoulation from repeated sampling
> 
> >
> 
> > ## Population size
> 
> > N <- 400
> 
> > N
> 
> >
> 
> > ## Vector labeling each item in the population
> 
> > pop <- c(1:N)
> 
> > pop
> 
> >
> 
> > ## Lower and upper bounds of sample size
> 
> > lower.bound <- round(x = .05 * N, digits = 0)
> 
> > lower.bound ## Smallest possible sample size
> 
> >
> 
> > upper.bound <- round(x = .15 * N, digits = 0)
> 
> > upper.bound ## Largest possible sample size
> 
> >
> 
> > ## Length of sample size interval
> 
> > length.ss.interval <- length(c(lower.bound:upper.bound))
> 
> > length.ss.interval ## total possible sample sizes, ranging form lower.bound
> 
> > to upper.bound
> 
> >
> 
> > ## Determine a sample size randomly (not a global variable...simply for
> 
> > test purposes)
> 
> > ## Between lower and upper bounds set previously
> 
> > ## Give equal weight to each possible sample size in this interval
> 
> > sample(x = c(lower.bound:upper.bound),
> 
> >   size = 1,
> 
> >   prob = c(rep(1/length.ss.interval, length.ss.interval)))
> 
> >
> 
> > ## Specify number of samples to take
> 
> > n.samples <- 100
> 
> >
> 
> > ## Initiate empty matrix
> 
> > ## 1st column is population (item 1 thorugh item 400)
> 
> > ## 2nd through nth column are all rounds of sampling
> 
> > dat <- matrix(data = NA,
> 
> >  nrow = length(pop),
> 
> >  ncol = n.samples + 1)
> 
> >
> 
> > dat[,1] <- pop
> 
> >
> 
> > dat
> 
> >
> 
> > ## Take samples of random sizes
> 
> > ## Record results in columns 2 through n
> 
> > ## 1 = sampled (marked)
> 
> > ## 0 = not sampled (not marked)
> 
> > for(i in 2:ncol(dat)) {
> 
> >  a.sample <- sample(x = pop,
> 
> > size = sample(x = c(lower.bound:upper.bound),
> 
> >   size = 1,
> 
> >   prob = c(rep(1/length.ss.interval,
> 
> > length.ss.interval))),
> 
> > replace = FALSE)
> 
> >  dat[,i] <- dat[,1] %in% a.sample
> 
> > }
> 
> >
> 
> > ## How large was each sample size?
> 
> > apply(X = dat, MARGIN 

Re: [R] Nested for loop

2017-08-06 Thread Kirsten Morehouse
Hi Ben,

That's exactly right! Except for each set it's the sample population that
is 400, 800 or 300. I want to take 3 samples, each of 100, where only the
population differs. I can do this separately, but I'm having trouble
putting them all on the same graph.

I'd like to have sample on the x axis (1-300) and estimate on the y axis. I
want to show how population affects the estimates.

Does this make more sense?

Thanks for your time!

Kirsten
On Sun, Aug 6, 2017 at 3:21 PM Ben Tupper  wrote:

> Hi Kirsten,
>
>
>
> I can run your example code but I can't quite follow your division of
> sampling.  Can you restate the the task?  Below is what I think you are
> asking for, but I have the feeling I may be off the mark.
>
>
>
>
>
> Set A: 400 samples, draw 100 in range of 5 to 15
>
>
>
> Set B: 800 samples, draw 100 in range of 5 to 15
>
>
>
> Set C: 300 samples, draw 100 in range of 5 to 15
>
>
>
> Ben
>
>
>
> > On Aug 5, 2017, at 9:21 AM, Kirsten Morehouse 
> wrote:
>
> >
>
> > Hi! Thanks for taking the time to read this.
>
> >
>
> > The code below creates a graph that takes 100 samples that are between 5%
>
> > and 15% of the population (400).
>
> >
>
> > What I'd like to do, however, is add two other sections to the graph. It
>
> > would look something like this:
>
> >
>
> > from 1-100 samples take 100 samples that are between 5% and 15% of the
>
> > population (400). From 101-200 take 100 samples that are between 5% and
> 15%
>
> > of the population (800). From 201-300 take 100 samples that are between
> 5%
>
> > and 15% of the population (300).
>
> >
>
> > I assume this would require a nested for loop. Does anyone have advice as
>
> > to how to do this?
>
> >
>
> > Thanks for your time. Kirsten
>
> >
>
> > ## Mark-Recapture
>
> > ## Estimate popoulation from repeated sampling
>
> >
>
> > ## Population size
>
> > N <- 400
>
> > N
>
> >
>
> > ## Vector labeling each item in the population
>
> > pop <- c(1:N)
>
> > pop
>
> >
>
> > ## Lower and upper bounds of sample size
>
> > lower.bound <- round(x = .05 * N, digits = 0)
>
> > lower.bound ## Smallest possible sample size
>
> >
>
> > upper.bound <- round(x = .15 * N, digits = 0)
>
> > upper.bound ## Largest possible sample size
>
> >
>
> > ## Length of sample size interval
>
> > length.ss.interval <- length(c(lower.bound:upper.bound))
>
> > length.ss.interval ## total possible sample sizes, ranging form
> lower.bound
>
> > to upper.bound
>
> >
>
> > ## Determine a sample size randomly (not a global variable...simply for
>
> > test purposes)
>
> > ## Between lower and upper bounds set previously
>
> > ## Give equal weight to each possible sample size in this interval
>
> > sample(x = c(lower.bound:upper.bound),
>
> >   size = 1,
>
> >   prob = c(rep(1/length.ss.interval, length.ss.interval)))
>
> >
>
> > ## Specify number of samples to take
>
> > n.samples <- 100
>
> >
>
> > ## Initiate empty matrix
>
> > ## 1st column is population (item 1 thorugh item 400)
>
> > ## 2nd through nth column are all rounds of sampling
>
> > dat <- matrix(data = NA,
>
> >  nrow = length(pop),
>
> >  ncol = n.samples + 1)
>
> >
>
> > dat[,1] <- pop
>
> >
>
> > dat
>
> >
>
> > ## Take samples of random sizes
>
> > ## Record results in columns 2 through n
>
> > ## 1 = sampled (marked)
>
> > ## 0 = not sampled (not marked)
>
> > for(i in 2:ncol(dat)) {
>
> >  a.sample <- sample(x = pop,
>
> > size = sample(x = c(lower.bound:upper.bound),
>
> >   size = 1,
>
> >   prob = c(rep(1/length.ss.interval,
>
> > length.ss.interval))),
>
> > replace = FALSE)
>
> >  dat[,i] <- dat[,1] %in% a.sample
>
> > }
>
> >
>
> > ## How large was each sample size?
>
> > apply(X = dat, MARGIN = 2, FUN = sum)
>
> > ## 1st element is irrelevant
>
> > ## 2nd element through nth element: sample size for each of the 100
> samples
>
> >
>
> > ## At this point, all computations can be done using dat
>
> >
>
> > ## Create Schnabel dataframe using dat
>
> > ## Google the Schnabel formula
>
> >
>
> > schnabel.comp <- data.frame(sample = 1:n.samples,
>
> >n.sampled = apply(X = dat, MARGIN = 2, FUN =
>
> > sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
>
> > )
>
> >
>
> > ## First column: which sample, 1-100
>
> > ## Second column: number selected in that sample
>
> >
>
> >
>
> > ## How many items were previously sampled?
>
> > ## For 1st sample, it's 0
>
> > ## For 2nd sample, code is different than for remaning samples
>
> >
>
> > n.prev.sampled <- c(0, rep(NA, n.samples-1))
>
> > n.prev.sampled
>
> >
>
> > n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
>
> >yes = 1,
>
> >no = 0))
>
> >
>
> > n.prev.sampled
>
> >
>
> > for(i in 4:ncol(dat)) {
>
> >  n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
>
> > 

Re: [R] Nested for loop

2017-08-06 Thread Ben Tupper
Hi Kirsten,

I can run your example code but I can't quite follow your division of sampling. 
 Can you restate the the task?  Below is what I think you are asking for, but I 
have the feeling I may be off the mark.


Set A: 400 samples, draw 100 in range of 5 to 15

Set B: 800 samples, draw 100 in range of 5 to 15

Set C: 300 samples, draw 100 in range of 5 to 15

Ben

> On Aug 5, 2017, at 9:21 AM, Kirsten Morehouse  wrote:
> 
> Hi! Thanks for taking the time to read this.
> 
> The code below creates a graph that takes 100 samples that are between 5%
> and 15% of the population (400).
> 
> What I'd like to do, however, is add two other sections to the graph. It
> would look something like this:
> 
> from 1-100 samples take 100 samples that are between 5% and 15% of the
> population (400). From 101-200 take 100 samples that are between 5% and 15%
> of the population (800). From 201-300 take 100 samples that are between 5%
> and 15% of the population (300).
> 
> I assume this would require a nested for loop. Does anyone have advice as
> to how to do this?
> 
> Thanks for your time. Kirsten
> 
> ## Mark-Recapture
> ## Estimate popoulation from repeated sampling
> 
> ## Population size
> N <- 400
> N
> 
> ## Vector labeling each item in the population
> pop <- c(1:N)
> pop
> 
> ## Lower and upper bounds of sample size
> lower.bound <- round(x = .05 * N, digits = 0)
> lower.bound ## Smallest possible sample size
> 
> upper.bound <- round(x = .15 * N, digits = 0)
> upper.bound ## Largest possible sample size
> 
> ## Length of sample size interval
> length.ss.interval <- length(c(lower.bound:upper.bound))
> length.ss.interval ## total possible sample sizes, ranging form lower.bound
> to upper.bound
> 
> ## Determine a sample size randomly (not a global variable...simply for
> test purposes)
> ## Between lower and upper bounds set previously
> ## Give equal weight to each possible sample size in this interval
> sample(x = c(lower.bound:upper.bound),
>   size = 1,
>   prob = c(rep(1/length.ss.interval, length.ss.interval)))
> 
> ## Specify number of samples to take
> n.samples <- 100
> 
> ## Initiate empty matrix
> ## 1st column is population (item 1 thorugh item 400)
> ## 2nd through nth column are all rounds of sampling
> dat <- matrix(data = NA,
>  nrow = length(pop),
>  ncol = n.samples + 1)
> 
> dat[,1] <- pop
> 
> dat
> 
> ## Take samples of random sizes
> ## Record results in columns 2 through n
> ## 1 = sampled (marked)
> ## 0 = not sampled (not marked)
> for(i in 2:ncol(dat)) {
>  a.sample <- sample(x = pop,
> size = sample(x = c(lower.bound:upper.bound),
>   size = 1,
>   prob = c(rep(1/length.ss.interval,
> length.ss.interval))),
> replace = FALSE)
>  dat[,i] <- dat[,1] %in% a.sample
> }
> 
> ## How large was each sample size?
> apply(X = dat, MARGIN = 2, FUN = sum)
> ## 1st element is irrelevant
> ## 2nd element through nth element: sample size for each of the 100 samples
> 
> ## At this point, all computations can be done using dat
> 
> ## Create Schnabel dataframe using dat
> ## Google the Schnabel formula
> 
> schnabel.comp <- data.frame(sample = 1:n.samples,
>n.sampled = apply(X = dat, MARGIN = 2, FUN =
> sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
> )
> 
> ## First column: which sample, 1-100
> ## Second column: number selected in that sample
> 
> 
> ## How many items were previously sampled?
> ## For 1st sample, it's 0
> ## For 2nd sample, code is different than for remaning samples
> 
> n.prev.sampled <- c(0, rep(NA, n.samples-1))
> n.prev.sampled
> 
> n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
>yes = 1,
>no = 0))
> 
> n.prev.sampled
> 
> for(i in 4:ncol(dat)) {
>  n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
> rowSums(dat[,2:(i-1)]) > 0,
>yes = 1,
>no = 0))
> }
> 
> schnabel.comp$n.prev.sampled <- n.prev.sampled
> 
> ## n.newly.sampled: in each sample, how many items were newly sampled?
> ## i.e., never seen before?
> schnabel.comp$n.newly.sampled <- with(schnabel.comp,
>  n.sampled - n.prev.sampled)
> 
> ## cum.sampled: how many total items have you seen?
> schnabel.comp$cum.sampled <- c(0,
> cumsum(schnabel.comp$n.newly.sampled)[2:n.samples-1])
> 
> ## numerator of schnabel formula
> schnabel.comp$numerator <- with(schnabel.comp,
>n.sampled * cum.sampled)
> 
> ## denominator of schnable formula is n.prev.sampled
> 
> ## pop.estimate -- after each sample (starting with 2nd -- need at least
> two samples)
> schnabel.comp$pop.estimate <- NA
> 
> for(i in 1:length(schnabel.comp$pop.estimate)) {
>  schnabel.comp$pop.estimate[i] <- sum(schnabel.comp$numerator[1:i]) /

Re: [R] nested for loop with data table

2017-05-06 Thread Ek Esawi
Thank you Jeff. Your idea, as i mentioned on my previous posting, did
indeed work. I read somewhere that both data table dplyr do great many
things and i plan to learn both as much as i can. Suggestions on this list
either get you the answer you are looking for or give you lead to an answer.

Thanks again

On Thu, May 4, 2017 at 12:04 AM, Jeff Newmiller 
wrote:

> You seem to be unaware of the "aggregate" data processing concept. There
> are many ways to accomplish aggregation, but I am not fluent in data.table
> methods but knowing the concept is the first step.
>
> Perhaps look closely at [1], or Google for data table aggregation yourself?
>
> [1] https://www.r-bloggers.com/efficient-aggregation-and-
> more-using-data-table/amp/
> --
> Sent from my phone. Please excuse my brevity.
>
> On May 3, 2017 8:17:21 AM PDT, Ek Esawi  wrote:
> >Thank you both Boris and Jim. Thank you, Boris, for advising to read
> >the
> >posting guide; I had and I just did.
> >
> >Jim’s idea is exactly what I want; however, I could not pass sset1,
> >sset2,
> >etc. to the j nested loop and collect the results in an vector.
> >
> >Here attached my code, file, and my question which should be clear now.
> >The
> >question again is instead of using separate loops for each sset1 and
> >sset2,
> >I want one nested loop? Because I have at least 10 subsets
> >(sset1,sset2,sset3…..sset10).
> >
> >Thanks again, EK
> >
> >
> >---The code--
> >
> >install.packages("data.table")
> >library(data.table)
> >File1 <-  "C:/Users/SampleData.csv"
> >DT <- fread(File1)
> >sset1 <- DT[Num<10<10]
> >sset2 <- DT[Num>10<15]
> >
> ># Count how many combinations of A,B,C,D,E,F in each subset
> >for ( i in 1:length(sset1)){
> >  aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
> >  bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
> >  cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
> >  counts <- c(aa, bb,cc)
> >}
> >
> >for ( i in 1:length(sset2)){
> >  aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
> >  bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
> >  cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
> >  counts <-  c(aa1,bb1,cc1)
> >}
> >
> >---The File
> >
> >   Num  Color Grade ValueMonth Day
> > 1:   1 yellow A20  May   1
> > 2:   2  green B25 June   2
> > 3:   3  green A10April   3
> > 4:   4  black A17   August   3
> > 5:   5red C 5 December   5
> > 6:   6 orange D 0  January  13
> > 7:   7 orange E12  January   5
> > 8:   8 orange F11 February   8
> > 9:   9 orange F99 July  23
> >10:  10 orange F70  May   7
> >11:  11  black A77 June  11
> >12:  12  green B87April  33
> >13:  13  black A79   August   9
> >14:  14  green A68 December  14
> >15:  15  black C90  January  31
> >16:  16  green D79  January  11
> >17:  17  black E   101 February  17
> >18:  18red F90 July  21
> >19:  19red F   112 February  13
> >20:  20red F   101 July  20
> >
> >On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:
> >
> >> I have a huge data file; a sample is listed below. I am using the
> >package
> >> data table to process the file and I am stuck on one issue and need
> >some
> >> feedback. I used fread to create a data table. Then I divided the
> >data
> >> table (named File1) into 10 general subsets using common table
> >commands
> >> such as:
> >>
> >>
> >>
> >> AAA <- File1[Num<5>15]
> >>
> >> BBB <- File1[Num>15<10]
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >>
> >>
> >> I wanted to divide and count each of the above subsets based on a set
> >of
> >> parameters common to all subsets. I did the following to go through
> >each
> >> subset and it works:
> >>
> >> For (I in 1: length (AAA)) {
> >>
> >>   aa <- c(AAA[color==”green”==”a”,month==”Januray”
> >> .N],[ AAA[color==”green”==”b”& month==”June”’ .N])
> >>
> >> }
> >>
> >>
> >>
> >> The question: I don’t want to have a separate loop for each subset
> >(10
> >> loops). Instead, I was hoping to have 2 nested loops in the form
> >below:
> >>
> >>
> >>
> >> For (I in 1:N)){
> >>
> >>   For (j in 1:M){
> >>
> >>
> >>
> >> }
> >>
> >> }
> >>
> >>
> >>
> >>  Sample
> >>
> >>
> >> Num
> >>
> >> Color
> >>
> >> Grade
> >>
> >> Value
> >>
> >> Month
> >>
> >> Day
> >>
> >> 1
> >>
> >> yellow
> >>
> >> A
> >>
> >> 20
> >>
> >> May
> >>
> >> 1
> >>
> >> 2
> >>
> >> green
> >>
> >> B
> >>
> >> 25
> >>
> >> June
> >>
> >> 2
> >>
> >> 3
> >>
> >> green
> >>
> >> A
> >>
> >> 10
> >>
> >> April
> >>
> >> 3
> >>
> >> 4
> >>
> >> black
> >>
> >> A
> >>
> >> 17
> >>
> >> August
> >>
> >> 3
> >>
> >> 5
> >>
> >> red
> >>
> >> C
> >>
> >> 5
> >>
> >> December
> >>
> >> 5
> >>
> >> 6
> >>
> >> orange
> >>
> >> D
> >>
> >> 0
> >>
> >> January
> >>
> >> 

Re: [R] nested for loop with data table

2017-05-04 Thread PIKAL Petr
Hi

better to present us your data by dput, so they can be directly used.

> dput(dat)
dat <- structure(list(Num = 1:20, Color = structure(c(5L, 2L, 2L, 1L,
4L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 4L, 4L, 4L
), .Label = c("black", "green", "orange", "red", "yellow"), class = "factor"),
Grade = structure(c(1L, 2L, 1L, 1L, 3L, 4L, 5L, 6L, 6L, 6L,
1L, 2L, 1L, 1L, 3L, 4L, 5L, 6L, 6L, 6L), .Label = c("A",
"B", "C", "D", "E", "F"), class = "factor"), value = c(20L,
25L, 10L, 17L, 5L, 0L, 12L, 11L, 99L, 70L, 77L, 87L, 79L,
68L, 90L, 79L, 101L, 90L, 112L, 101L), Month = structure(c(8L,
7L, 1L, 2L, 3L, 5L, 5L, 4L, 6L, 8L, 7L, 1L, 2L, 3L, 5L, 5L,
4L, 6L, 4L, 6L), .Label = c("April", "August", "December",
"February", "January", "July", "June", "May"), class = "factor"),
Day = c(1L, 2L, 3L, 3L, 5L, 13L, 5L, 8L, 23L, 7L, 11L, 33L,
9L, 14L, 31L, 11L, 17L, 21L, 13L, 20L)), .Names = c("Num",
"Color", "Grade", "value", "Month", "Day"), class = "data.frame", row.names = 
c(NA,
-20L))
>

I do not know your exact intention and data.table commands. You can get some 
summary numbers simply by

table(dat$Grade[dat$Num<10 & dat$Day<10])

A B C D E F
3 1 1 0 1 1

It is probably preferable to obtain logical vectors for Num and Day before 
starting tabulation.

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ek Esawi
> Sent: Wednesday, May 3, 2017 5:17 PM
> To: r-help@r-project.org
> Subject: Re: [R] nested for loop with data table
>
> Thank you both Boris and Jim. Thank you, Boris, for advising to read the
> posting guide; I had and I just did.
>
> Jim’s idea is exactly what I want; however, I could not pass sset1, sset2, 
> etc.
> to the j nested loop and collect the results in an vector.
>
> Here attached my code, file, and my question which should be clear now.
> The question again is instead of using separate loops for each sset1 and
> sset2, I want one nested loop? Because I have at least 10 subsets
> (sset1,sset2,sset3…..sset10).
>
> Thanks again, EK
>
>
> ---The code--
>
> install.packages("data.table")
> library(data.table)
> File1 <-  "C:/Users/SampleData.csv"
> DT <- fread(File1)
> sset1 <- DT[Num<10<10]
> sset2 <- DT[Num>10<15]
>
> # Count how many combinations of A,B,C,D,E,F in each subset for ( i in
> 1:length(sset1)){
>   aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
>   bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
>   cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
>   counts <- c(aa, bb,cc)
> }
>
> for ( i in 1:length(sset2)){
>   aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
>   bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
>   cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
>   counts <-  c(aa1,bb1,cc1)
> }
>
> ---The File
>
>Num  Color Grade ValueMonth Day
>  1:   1 yellow A20  May   1
>  2:   2  green B25 June   2
>  3:   3  green A10April   3
>  4:   4  black A17   August   3
>  5:   5red C 5 December   5
>  6:   6 orange D 0  January  13
>  7:   7 orange E12  January   5
>  8:   8 orange F11 February   8
>  9:   9 orange F99 July  23
> 10:  10 orange F70  May   7
> 11:  11  black A77 June  11
> 12:  12  green B87April  33
> 13:  13  black A79   August   9
> 14:  14  green A68 December  14
> 15:  15  black C90  January  31
> 16:  16  green D79  January  11
> 17:  17  black E   101 February  17
> 18:  18red F90 July  21
> 19:  19red F   112 February  13
> 20:  20red F   101 July  20
>
> On Tue, May 2, 2017 at 12:35 PM, Ek Esawi <esaw...@gmail.com> wrote:
>
> > I have a huge data file; a sample is listed below. I am using the
> > package data table to process the file and I am stuck on one issue and
> > need some feedback. I used fread to create a data table. Then I
> > divided the data table (named File1) into 10 general subsets using
> > common table commands such as:
> >
> >
> >
> > AAA <- File1[Num<5>15]
> >
> > BBB <- File1[Num>15<10]
> >
> 

Re: [R] nested for loop with data table

2017-05-03 Thread Jeff Newmiller
You seem to be unaware of the "aggregate" data processing concept. There are 
many ways to accomplish aggregation, but I am not fluent in data.table methods 
but knowing the concept is the first step.

Perhaps look closely at [1], or Google for data table aggregation yourself? 

[1] 
https://www.r-bloggers.com/efficient-aggregation-and-more-using-data-table/amp/
-- 
Sent from my phone. Please excuse my brevity.

On May 3, 2017 8:17:21 AM PDT, Ek Esawi  wrote:
>Thank you both Boris and Jim. Thank you, Boris, for advising to read
>the
>posting guide; I had and I just did.
>
>Jim’s idea is exactly what I want; however, I could not pass sset1,
>sset2,
>etc. to the j nested loop and collect the results in an vector.
>
>Here attached my code, file, and my question which should be clear now.
>The
>question again is instead of using separate loops for each sset1 and
>sset2,
>I want one nested loop? Because I have at least 10 subsets
>(sset1,sset2,sset3…..sset10).
>
>Thanks again, EK
>
>
>---The code--
>
>install.packages("data.table")
>library(data.table)
>File1 <-  "C:/Users/SampleData.csv"
>DT <- fread(File1)
>sset1 <- DT[Num<10<10]
>sset2 <- DT[Num>10<15]
>
># Count how many combinations of A,B,C,D,E,F in each subset
>for ( i in 1:length(sset1)){
>  aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
>  bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
>  cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
>  counts <- c(aa, bb,cc)
>}
>
>for ( i in 1:length(sset2)){
>  aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
>  bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
>  cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
>  counts <-  c(aa1,bb1,cc1)
>}
>
>---The File
>
>   Num  Color Grade ValueMonth Day
> 1:   1 yellow A20  May   1
> 2:   2  green B25 June   2
> 3:   3  green A10April   3
> 4:   4  black A17   August   3
> 5:   5red C 5 December   5
> 6:   6 orange D 0  January  13
> 7:   7 orange E12  January   5
> 8:   8 orange F11 February   8
> 9:   9 orange F99 July  23
>10:  10 orange F70  May   7
>11:  11  black A77 June  11
>12:  12  green B87April  33
>13:  13  black A79   August   9
>14:  14  green A68 December  14
>15:  15  black C90  January  31
>16:  16  green D79  January  11
>17:  17  black E   101 February  17
>18:  18red F90 July  21
>19:  19red F   112 February  13
>20:  20red F   101 July  20
>
>On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:
>
>> I have a huge data file; a sample is listed below. I am using the
>package
>> data table to process the file and I am stuck on one issue and need
>some
>> feedback. I used fread to create a data table. Then I divided the
>data
>> table (named File1) into 10 general subsets using common table
>commands
>> such as:
>>
>>
>>
>> AAA <- File1[Num<5>15]
>>
>> BBB <- File1[Num>15<10]
>>
>> …..
>>
>> …..
>>
>> …..
>>
>> …..
>>
>> …..
>>
>> …..
>>
>>
>>
>> I wanted to divide and count each of the above subsets based on a set
>of
>> parameters common to all subsets. I did the following to go through
>each
>> subset and it works:
>>
>> For (I in 1: length (AAA)) {
>>
>>   aa <- c(AAA[color==”green”==”a”,month==”Januray”
>> .N],[ AAA[color==”green”==”b”& month==”June”’ .N])
>>
>> }
>>
>>
>>
>> The question: I don’t want to have a separate loop for each subset
>(10
>> loops). Instead, I was hoping to have 2 nested loops in the form
>below:
>>
>>
>>
>> For (I in 1:N)){
>>
>>   For (j in 1:M){
>>
>>
>>
>> }
>>
>> }
>>
>>
>>
>>  Sample
>>
>>
>> Num
>>
>> Color
>>
>> Grade
>>
>> Value
>>
>> Month
>>
>> Day
>>
>> 1
>>
>> yellow
>>
>> A
>>
>> 20
>>
>> May
>>
>> 1
>>
>> 2
>>
>> green
>>
>> B
>>
>> 25
>>
>> June
>>
>> 2
>>
>> 3
>>
>> green
>>
>> A
>>
>> 10
>>
>> April
>>
>> 3
>>
>> 4
>>
>> black
>>
>> A
>>
>> 17
>>
>> August
>>
>> 3
>>
>> 5
>>
>> red
>>
>> C
>>
>> 5
>>
>> December
>>
>> 5
>>
>> 6
>>
>> orange
>>
>> D
>>
>> 0
>>
>> January
>>
>> 13
>>
>> 7
>>
>> orange
>>
>> E
>>
>> 12
>>
>> January
>>
>> 5
>>
>> 8
>>
>> orange
>>
>> F
>>
>> 11
>>
>> February
>>
>> 8
>>
>> 9
>>
>> orange
>>
>> F
>>
>> 99
>>
>> July
>>
>> 23
>>
>> 10
>>
>> orange
>>
>> F
>>
>> 70
>>
>> May
>>
>> 7
>>
>> 11
>>
>> black
>>
>> A
>>
>> 77
>>
>> June
>>
>> 11
>>
>> 12
>>
>> green
>>
>> B
>>
>> 87
>>
>> April
>>
>> 33
>>
>> 13
>>
>> black
>>
>> A
>>
>> 79
>>
>> August
>>
>> 9
>>
>> 14
>>
>> green
>>
>> A
>>
>> 68
>>
>> December
>>
>> 14
>>
>> 15
>>
>> black
>>
>> C
>>
>> 90
>>
>> January
>>
>> 31
>>
>> 16
>>
>> green
>>
>> D
>>
>> 79
>>
>> January
>>
>> 11
>>
>> 17
>>
>> black
>>
>> E
>>
>> 101
>>
>> February
>>
>> 17
>>
>> 18
>>
>> red
>>
>> F
>>
>> 90
>>
>> July
>>
>> 21
>>
>> 19
>>
>> red
>>
>> F
>>
>> 112
>>
>> February
>>
>> 13
>>
>> 20
>>
>> red
>>
>> F
>>
>> 101
>>
>> July
>>
>> 20
>>
>>

Re: [R] nested for loop with data table

2017-05-03 Thread Ek Esawi
Thank you both Boris and Jim. Thank you, Boris, for advising to read the
posting guide; I had and I just did.

Jim’s idea is exactly what I want; however, I could not pass sset1, sset2,
etc. to the j nested loop and collect the results in an vector.

Here attached my code, file, and my question which should be clear now. The
question again is instead of using separate loops for each sset1 and sset2,
I want one nested loop? Because I have at least 10 subsets
(sset1,sset2,sset3…..sset10).

Thanks again, EK


---The code--

install.packages("data.table")
library(data.table)
File1 <-  "C:/Users/SampleData.csv"
DT <- fread(File1)
sset1 <- DT[Num<10<10]
sset2 <- DT[Num>10<15]

# Count how many combinations of A,B,C,D,E,F in each subset
for ( i in 1:length(sset1)){
  aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
  bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
  cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
  counts <- c(aa, bb,cc)
}

for ( i in 1:length(sset2)){
  aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
  bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
  cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
  counts <-  c(aa1,bb1,cc1)
}

---The File

   Num  Color Grade ValueMonth Day
 1:   1 yellow A20  May   1
 2:   2  green B25 June   2
 3:   3  green A10April   3
 4:   4  black A17   August   3
 5:   5red C 5 December   5
 6:   6 orange D 0  January  13
 7:   7 orange E12  January   5
 8:   8 orange F11 February   8
 9:   9 orange F99 July  23
10:  10 orange F70  May   7
11:  11  black A77 June  11
12:  12  green B87April  33
13:  13  black A79   August   9
14:  14  green A68 December  14
15:  15  black C90  January  31
16:  16  green D79  January  11
17:  17  black E   101 February  17
18:  18red F90 July  21
19:  19red F   112 February  13
20:  20red F   101 July  20

On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:

> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
>
>
>
> AAA <- File1[Num<5>15]
>
> BBB <- File1[Num>15<10]
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
>
>
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
>
> For (I in 1: length (AAA)) {
>
>   aa <- c(AAA[color==”green”==”a”,month==”Januray”
> .N],[ AAA[color==”green”==”b”& month==”June”’ .N])
>
> }
>
>
>
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
>
>
>
> For (I in 1:N)){
>
>   For (j in 1:M){
>
>
>
> }
>
> }
>
>
>
>  Sample
>
>
> Num
>
> Color
>
> Grade
>
> Value
>
> Month
>
> Day
>
> 1
>
> yellow
>
> A
>
> 20
>
> May
>
> 1
>
> 2
>
> green
>
> B
>
> 25
>
> June
>
> 2
>
> 3
>
> green
>
> A
>
> 10
>
> April
>
> 3
>
> 4
>
> black
>
> A
>
> 17
>
> August
>
> 3
>
> 5
>
> red
>
> C
>
> 5
>
> December
>
> 5
>
> 6
>
> orange
>
> D
>
> 0
>
> January
>
> 13
>
> 7
>
> orange
>
> E
>
> 12
>
> January
>
> 5
>
> 8
>
> orange
>
> F
>
> 11
>
> February
>
> 8
>
> 9
>
> orange
>
> F
>
> 99
>
> July
>
> 23
>
> 10
>
> orange
>
> F
>
> 70
>
> May
>
> 7
>
> 11
>
> black
>
> A
>
> 77
>
> June
>
> 11
>
> 12
>
> green
>
> B
>
> 87
>
> April
>
> 33
>
> 13
>
> black
>
> A
>
> 79
>
> August
>
> 9
>
> 14
>
> green
>
> A
>
> 68
>
> December
>
> 14
>
> 15
>
> black
>
> C
>
> 90
>
> January
>
> 31
>
> 16
>
> green
>
> D
>
> 79
>
> January
>
> 11
>
> 17
>
> black
>
> E
>
> 101
>
> February
>
> 17
>
> 18
>
> red
>
> F
>
> 90
>
> July
>
> 21
>
> 19
>
> red
>
> F
>
> 112
>
> February
>
> 13
>
> 20
>
> red
>
> F
>
> 101
>
> July
>
> 20
>
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] nested for loop with data table

2017-05-02 Thread Jim Lemon
Hi Ek,
I think you want your example to look like this:

Sample<-read.table(text=
"Num Color Grade Value Month Day
1 yellow A 20 May 1
2 green B 25 June 2
3 green A 10 April 3
4 black A 17 August 3
5 red C 5 December 5
6 orange D 0 January 13
7 orange E 12 January 5
8 orange F 11 February 8
9 orange F 99 July 23
10 orange F 70 May 7
11 black A 77 June 11
12 green B 87 April 33
13 black A 79 August 9
14 green A 68 December 14
15 black C 90 January 31
16 green D 79 January 11
17 black E 101 February 17
18 red F 90 July 21
19 red F 112 February 13
20 red F 101 July 20",
header=TRUE)
AAA<-Sample[Sample$Num < 5 & Sample$Day < 3,]
BBB<-Sample[Sample$Num > 15 & Sample$Day > 13,]
for(i in 1:length(AAA)) {
 for(j in 1:length(BBB)) {
  ...
 }
}

except in data.table notation. However, I can't work out what you want
to do in the loop.

Jim


On Wed, May 3, 2017 at 2:35 AM, Ek Esawi  wrote:
> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
>
>
>
> AAA <- File1[Num<5>15]
>
> BBB <- File1[Num>15<10]
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
>
>
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
>
> For (I in 1: length (AAA)) {
>
>   aa <- c(AAA[color==”green”==”a”,month==”Januray” .N],[
> AAA[color==”green”==”b”& month==”June”’ .N])
>
> }
>
>
>
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
>
>
>
> For (I in 1:N)){
>
>   For (j in 1:M){
>
>
>
> }
>
> }
>
>
>
>  Sample
>
>
> Num
>
> Color
>
> Grade
>
> Value
>
> Month
>
> Day
>
> 1
>
> yellow
>
> A
>
> 20
>
> May
>
> 1
>
> 2
>
> green
>
> B
>
> 25
>
> June
>
> 2
>
> 3
>
> green
>
> A
>
> 10
>
> April
>
> 3
>
> 4
>
> black
>
> A
>
> 17
>
> August
>
> 3
>
> 5
>
> red
>
> C
>
> 5
>
> December
>
> 5
>
> 6
>
> orange
>
> D
>
> 0
>
> January
>
> 13
>
> 7
>
> orange
>
> E
>
> 12
>
> January
>
> 5
>
> 8
>
> orange
>
> F
>
> 11
>
> February
>
> 8
>
> 9
>
> orange
>
> F
>
> 99
>
> July
>
> 23
>
> 10
>
> orange
>
> F
>
> 70
>
> May
>
> 7
>
> 11
>
> black
>
> A
>
> 77
>
> June
>
> 11
>
> 12
>
> green
>
> B
>
> 87
>
> April
>
> 33
>
> 13
>
> black
>
> A
>
> 79
>
> August
>
> 9
>
> 14
>
> green
>
> A
>
> 68
>
> December
>
> 14
>
> 15
>
> black
>
> C
>
> 90
>
> January
>
> 31
>
> 16
>
> green
>
> D
>
> 79
>
> January
>
> 11
>
> 17
>
> black
>
> E
>
> 101
>
> February
>
> 17
>
> 18
>
> red
>
> F
>
> 90
>
> July
>
> 21
>
> 19
>
> red
>
> F
>
> 112
>
> February
>
> 13
>
> 20
>
> red
>
> F
>
> 101
>
> July
>
> 20
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] nested for loop with data table

2017-05-02 Thread Boris Steipe
There's a lot that doesn't make sense here. I think what you need to do is 
produce a small, reproducible example, post that with dput() and state your 
question more clearly - including what you have tried and what didn't work. 
You'll probably be amazed how quickly you will get good advice if 
_you_only_follow_the_posting_guide_.

B.




> On May 2, 2017, at 12:35 PM, Ek Esawi  wrote:
> 
> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
> 
> 
> 
> AAA <- File1[Num<5>15]
> 
> BBB <- File1[Num>15<10]
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> 
> 
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
> 
> For (I in 1: length (AAA)) {
> 
>  aa <- c(AAA[color==”green”==”a”,month==”Januray” .N],[
> AAA[color==”green”==”b”& month==”June”’ .N])
> 
> }
> 
> 
> 
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
> 
> 
> 
> For (I in 1:N)){
> 
>  For (j in 1:M){
> 
> 
> 
> }
> 
> }
> 
> 
> 
> Sample
> 
> 
> Num
> 
> Color
> 
> Grade
> 
> Value
> 
> Month
> 
> Day
> 
> 1
> 
> yellow
> 
> A
> 
> 20
> 
> May
> 
> 1
> 
> 2
> 
> green
> 
> B
> 
> 25
> 
> June
> 
> 2
> 
> 3
> 
> green
> 
> A
> 
> 10
> 
> April
> 
> 3
> 
> 4
> 
> black
> 
> A
> 
> 17
> 
> August
> 
> 3
> 
> 5
> 
> red
> 
> C
> 
> 5
> 
> December
> 
> 5
> 
> 6
> 
> orange
> 
> D
> 
> 0
> 
> January
> 
> 13
> 
> 7
> 
> orange
> 
> E
> 
> 12
> 
> January
> 
> 5
> 
> 8
> 
> orange
> 
> F
> 
> 11
> 
> February
> 
> 8
> 
> 9
> 
> orange
> 
> F
> 
> 99
> 
> July
> 
> 23
> 
> 10
> 
> orange
> 
> F
> 
> 70
> 
> May
> 
> 7
> 
> 11
> 
> black
> 
> A
> 
> 77
> 
> June
> 
> 11
> 
> 12
> 
> green
> 
> B
> 
> 87
> 
> April
> 
> 33
> 
> 13
> 
> black
> 
> A
> 
> 79
> 
> August
> 
> 9
> 
> 14
> 
> green
> 
> A
> 
> 68
> 
> December
> 
> 14
> 
> 15
> 
> black
> 
> C
> 
> 90
> 
> January
> 
> 31
> 
> 16
> 
> green
> 
> D
> 
> 79
> 
> January
> 
> 11
> 
> 17
> 
> black
> 
> E
> 
> 101
> 
> February
> 
> 17
> 
> 18
> 
> red
> 
> F
> 
> 90
> 
> July
> 
> 21
> 
> 19
> 
> red
> 
> F
> 
> 112
> 
> February
> 
> 13
> 
> 20
> 
> red
> 
> F
> 
> 101
> 
> July
> 
> 20
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] nested structure for Ancova

2017-03-12 Thread li li
Thanks so much, Richard. That  works.
 Hanna

2017-03-12 14:31 GMT-04:00 Richard M. Heiberger :

> I think you need either
>
> mod4 <- lm( y ~ -1 + area / (month*group), data=two)
>
> mod5 <- lm( y ~ area / (month*group), data=two)
>
> With either of those,  area:month and area:group and Residuals add up.
>
>
> On Sun, Mar 12, 2017 at 10:39 li li  wrote:
>
>> Hi All,
>>   I have a dataset which contains 4 variables: area, group, time, y,
>> Area is a factor that has two levels A and B, group is a factor that is
>> nested within area. There are four groups within each area.
>> y is the response variable, and time refers to different days.
>>
>>   Below is the how data looks like.
>>
>>   First, I fit separate ancova for each area (area A and B), For each
>> area,
>> I obtained different regression lines for each group within that area.
>>
>>   mod1 <- lm(y ~ month * group, data=two[two$area=="A"]
>>
>>   mod2 <- lm(y ~ month * group, data=two[two$area=="B"]
>>
>>  I want to fit the model at one time by using the nested structure
>>
>>  mod3 <-  lm(y ~ -1+ month * (area/group), data=two)
>>
>> I get different results using mod 3 from using mod1 and mod2.
>>
>>   Can someone give some suggestion on this.  How can I specify the model
>> in
>> R to simultaneously fit the model to get the same results as from mod1 and
>> mod2.
>>
>> Thanks  very much!
>> Hanna
>>
>> > head(two[two$area=="A",])   group time y area
>> 79 13 -1.394327A
>> 80 16 -1.435485A
>> 81 19 -1.406497A
>> 82 1   12 -1.265848A
>> 83 10 -1.316768A
>> 84 16 -1.431292A> head(two[two$area=="B",])  group time
>>  y area
>> 1 10 -2.145581B
>> 2 10 -1.910543B
>> 3 10 -2.128632B
>> 4 13 -2.079442B
>> 5 13 -2.273026B
>> 6 16 -2.312635B
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested structure for Ancova

2017-03-12 Thread Richard M. Heiberger
I think you need either

mod4 <- lm( y ~ -1 + area / (month*group), data=two)

mod5 <- lm( y ~ area / (month*group), data=two)

With either of those,  area:month and area:group and Residuals add up.


On Sun, Mar 12, 2017 at 10:39 li li  wrote:

> Hi All,
>   I have a dataset which contains 4 variables: area, group, time, y,
> Area is a factor that has two levels A and B, group is a factor that is
> nested within area. There are four groups within each area.
> y is the response variable, and time refers to different days.
>
>   Below is the how data looks like.
>
>   First, I fit separate ancova for each area (area A and B), For each area,
> I obtained different regression lines for each group within that area.
>
>   mod1 <- lm(y ~ month * group, data=two[two$area=="A"]
>
>   mod2 <- lm(y ~ month * group, data=two[two$area=="B"]
>
>  I want to fit the model at one time by using the nested structure
>
>  mod3 <-  lm(y ~ -1+ month * (area/group), data=two)
>
> I get different results using mod 3 from using mod1 and mod2.
>
>   Can someone give some suggestion on this.  How can I specify the model in
> R to simultaneously fit the model to get the same results as from mod1 and
> mod2.
>
> Thanks  very much!
> Hanna
>
> > head(two[two$area=="A",])   group time y area
> 79 13 -1.394327A
> 80 16 -1.435485A
> 81 19 -1.406497A
> 82 1   12 -1.265848A
> 83 10 -1.316768A
> 84 16 -1.431292A> head(two[two$area=="B",])  group time
>  y area
> 1 10 -2.145581B
> 2 10 -1.910543B
> 3 10 -2.128632B
> 4 13 -2.079442B
> 5 13 -2.273026B
> 6 16 -2.312635B
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested structure for Ancova

2017-03-12 Thread Michael Dewey

Dear Hannah

You say they are different (mod3 from mod1 and mod2) but in what way are 
the results different?


On 12/03/2017 15:38, li li wrote:

Hi All,
  I have a dataset which contains 4 variables: area, group, time, y,
Area is a factor that has two levels A and B, group is a factor that is
nested within area. There are four groups within each area.
y is the response variable, and time refers to different days.

  Below is the how data looks like.

  First, I fit separate ancova for each area (area A and B), For each area,
I obtained different regression lines for each group within that area.

  mod1 <- lm(y ~ month * group, data=two[two$area=="A"]

  mod2 <- lm(y ~ month * group, data=two[two$area=="B"]

 I want to fit the model at one time by using the nested structure

 mod3 <-  lm(y ~ -1+ month * (area/group), data=two)

I get different results using mod 3 from using mod1 and mod2.

  Can someone give some suggestion on this.  How can I specify the model in
R to simultaneously fit the model to get the same results as from mod1 and
mod2.

Thanks  very much!
Hanna


head(two[two$area=="A",])   group time y area

79 13 -1.394327A
80 16 -1.435485A
81 19 -1.406497A
82 1   12 -1.265848A
83 10 -1.316768A
84 16 -1.431292A> head(two[two$area=="B",])  group time
 y area
1 10 -2.145581B
2 10 -1.910543B
3 10 -2.128632B
4 13 -2.079442B
5 13 -2.273026B
6 16 -2.312635B

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



--
Michael
http://www.dewey.myzen.co.uk/home.html

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested Avova: Unequal # of Observations

2015-12-21 Thread Bert Gunter
Looks like homework. If so, you should know that there is a no
homework policy on this list.

If not, it looks like you should do some on your own by
?anova
?t.test

And perhaps some of:

(a) Consulting a statistics text;
(b) Going through an R tutorial (there are many good ones on the Web);
(c) Showing us what you have done and what errors have occurred. You
should not expect us to do your work for you, at least IMHO (others
may differ).

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Dec 21, 2015 at 3:10 PM, Sidoti, Salvatore A.
 wrote:
> I have two experimental groups (treatment & control) with 6 sets of 
> observations nested within each group. The number of observations in each set 
> is not equal.
>
>
> How do I set up a such an ANOVA in R?
>
>
> Thank You!
>
>
> Salvatore Sidoti
>
> PhD Student
>
> Graduate Teaching Assistant
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested ANOVA yields surprising results

2015-10-31 Thread peter dalgaard

> On 30 Oct 2015, at 18:46 , Daniel Wagenaar  wrote:
> 
> Dear R users:
> 
> All textbook references that I consult say that in a nested ANOVA (e.g., 
> A/B), the F statistic for factor A should be calculated as
> 
> F_A = MS_A / MS_(B within A).
> 

That would depend on which hypothesis you test in which model. If a reference 
tells you that you "should" do something without specifying the model, then you 
"should" look at a different reference.

In general, having anything other than the residual MS in the denominator 
indicates that you think it represents an additional source of random 
variation. I don't think that is invariably the case in nested designs (and, by 
the way, notice that "nested" is used differently by different books and 
software).

If you don't say otherwise, R assumes that there is only one source of random 
variation the model - a single error term if you like - and that all other 
terms represent systematic variations. In this mode of thinking, an A:B term 
represents an effect of B within A (additive and interaction effects combined), 
and you can test for its presence by comparing MS_A:B to MS_res. In its 
absence, you might choose to reduce the model and next look for an effect of A; 
purists would do this by comparing MS_A to the new MS_res obtained by pooling 
MS_A:B and MS_res, but lazy statisticians/programmers have found it more 
convenient to stick with the original MS_res denominator throughout (to get the 
pooling done, just fit the reduced model). 

If you want A:B to be a random term, then you need to say so, e.g. using

> summary(aov(Y~A+Error(A:B-1)))

Error: A:B
Df Sum Sq Mean Sq F value Pr(>F)
A2 0.4735  0.2367   0.4030.7
Residuals3 1.7635  0.5878   

Error: Within
  Df Sum Sq Mean Sq F value Pr(>F)
Residuals  6  4.993  0.8322   

(the -1 in the Error() term prevents an error message, which as far as I can 
tell is spurious).

Notice that you need aov() for this; lm() doesn't do Error() terms. This _only_ 
works in balanced designs.

-pd

> But when I run this simple example:
> 
> set.seed(1)
> A <- factor(rep(1:3, each=4))
> B <- factor(rep(1:2, 3, each=2))
> Y <- rnorm(12)
> anova(lm(Y ~ A/B))
> 
> I get this result:
> 
>  Analysis of Variance Table
> 
>  Response: Y
>Df Sum Sq Mean Sq F value Pr(>F)
>  A  2 0.4735 0.23675  0.2845 0.7620
>  A:B3 1.7635 0.58783  0.7064 0.5823
>  Residuals  6 4.9931 0.83218
> 
> Evidently, R calculates the F value for A as MS_A / MS_Residuals.
> 
> While it is straightforward enough to calculate what I think is the correct 
> result from the table, I am surprised that R doesn't give me that answer 
> directly. Does anybody know if R's behavior is intentional, and if so, why? 
> Equally importantly, is there a straightforward way to make R give the answer 
> I expect, that is:
> 
> Df Sum Sq Mean Sq F value Pr(>F)
>  A   2 0.4735 0.23675  0.4028 0.6999
> 
> The students in my statistics class would be much happier if they didn't have 
> to type things like
> 
>  a <- anova(...)
>  F <- a$`Sum Sq`[1] / a$`Sum Sq`[2]
>  P <- 1 - pf(F, a$Df[1], a$Df[2])
> 
> (They are not R programmers (yet).) And to be honest, I would find it easier 
> to read those results directly from the table as well.
> 
> Thanks,
> 
> Daniel Wagenaar
> 
> -- 
> Daniel A. Wagenaar, PhD
> Assistant Professor
> Department of Biological Sciences
> McMicken College of Arts and Sciences
> University of Cincinnati
> Cincinnati, OH 45221
> Phone: +1 (513) 556-9757
> Email: daniel.wagen...@uc.edu
> Web: http://www.danielwagenaar.net
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Nested effects (was: "no subject")

2015-10-30 Thread Rolf Turner

On 31/10/15 03:32, Wagenaar, Daniel (wagenadl) wrote:

Dear R users:

All textbook references that I consult say that in a nested ANOVA
(e.g., A/B), the F statistic for factor A should be calculated as F_A =
MS_A / MS_(B within A). But when I run this simple example:

set.seed(1)
A = factor(rep(1:3, each=4))
B = factor(rep(1:2, 3, each=2))
Y = rnorm(12)
anova(lm(Y ~ A/B))

I get this result:




Analysis of Variance Table

Response: Y Df Sum Sq Mean Sq F value Pr(>F) A 2 0.4735 0.23675
0.2845 0.7620 A:B 3 1.7635 0.58783 0.7064 0.5823 Residuals 6 4.9931
0.83218

Evidently, R calculates the F value for A as MS_A / MS_Residuals.
While it is straightforward enough to calculate what I think is the
correct result from the table, I am surprised that R doesn't give me
that answer directly. Does anybody know if R's behavior is intentional,
and if so, why? And, perhaps most importantly, how to get the "textbook"
result in the most straightforward way? (I'd like to be able to give me
students a simple procedure...)


The formula that you specify is based upon factor "B" being a *random* 
effect.  The lm() function handles *fixed* effects only, and thus treats 
"B" as a fixed effect --- whether this makes any sense or not is another 
story.  (IMHO only random effects make sense as nested effects.)


Kevin Wright has already told you how to get what you want/need using 
aov() and the Error() function.  This works only for balanced designs, 
essentially.  For more complicated designs you will need to dive into 
the nlme and lme4 packages.  For which you will need *lots* of patience, 
determination, and luck! :-)


cheers,

Rolf Turner

P. S. Please provide a useful *subject line* in your posts to this list.

R. T.

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested model and post hoc?

2015-08-05 Thread Bert Gunter
I rather suspect the problem is primarily statistical, not R related. If at
all possible, try to get some local statistical advice. Most probably, you
have empty cells in some of the dpi.f by diss.f table.

Also, using a Gaussian family and the cells/mg response may be
inappropriate: 1 cell  in 5 mg is different than 100 cells in 500 mg. This
obviously depends on your data, which is why local help might be important.

Cheers,
Bert


On Wednesday, August 5, 2015, a_wohl sithlo...@gmx.net wrote:

 Hi :-) i am really looking forward to get some help... Since I am a
 R-beginner I need it.

 I have a model consisting of the factors: dpi, infection, dissection day
 and
 plate. My reskponse variable is cells/mg. dpi is nested in dissection day.
 They are all fixed variables.
 I produced this nested model (best AIC index)
 glm.3-cm.2~infect.f+dpi.f+dpi.f/diss.f, na.action=na.omit)

 summary(glm.3)
 Call:
 glm(formula = cm.2 ~ infect.f + dpi.f + dpi.f %in% diss.f, family =
 gaussian,
 na.action = na.omit)

 Deviance Residuals:
 Min   1Q   Median   3Q  Max
 -2.7746  -0.3625   0.0080   0.3628   1.9031

 Coefficients: (18 not defined because of singularities)
 Estimate Std. Error t value Pr(|t|)
 (Intercept)  7.018870.15278  45.941   2e-16 ***
 infect.finf  0.055440.08107   0.684   0.4946
 infect.fsha -0.155630.08831  -1.762   0.0791 .
 dpi.f2   0.089240.20459   0.436   0.6630
 dpi.f4  -0.401670.21082  -1.905   0.0577 .
 dpi.f8   0.019610.20789   0.094   0.9249
 dpi.f16  0.822100.20469   4.016 7.54e-05 ***
 dpi.f32  0.996390.21435   4.648 5.09e-06 ***
 dpi.f1:diss.f2   0.132760.21077   0.630   0.5293
 dpi.f2:diss.f2NA NA  NA   NA
 dpi.f4:diss.f2   0.467490.20784   2.249   0.0253 *
 dpi.f8:diss.f2  -0.065550.20205  -0.324   0.7459
 dpi.f16:diss.f2   NA NA  NA   NA
 dpi.f32:diss.f2   NA NA  NA   NA
 dpi.f1:diss.f3NA NA  NA   NA
 dpi.f2:diss.f3  -0.157940.20186  -0.782   0.4346
 dpi.f4:diss.f3   0.209210.21081   0.992   0.3218
 dpi.f8:diss.f3NA NA  NA   NA
 dpi.f16:diss.f3 -0.036110.20178  -0.179   0.8581
 dpi.f32:diss.f3   NA NA  NA   NA
 dpi.f1:diss.f4   0.140400.20459   0.686   0.4931
 dpi.f2:diss.f4  -0.017010.21152  -0.080   0.9360
 dpi.f4:diss.f4NA NA  NA   NA
 dpi.f8:diss.f4NA NA  NA   NA
 dpi.f16:diss.f4 -0.190820.20168  -0.946   0.3449
 dpi.f32:diss.f4   NA NA  NA   NA
 dpi.f1:diss.f5NA NA  NA   NA
 dpi.f2:diss.f5NA NA  NA   NA

 and so forth.
 Since i decided for a nested modul, I wonder where the NAs are coming from?
 Since I want to perform a post hoc test, I was trysing the glht fuinction
 form the multcomp package.
 I always get this error
 Error in modelparm.default(model, ...) :
   dimensions of coefficients and covariance matrix don't match

 I think this is due to my NAs in the summary(glm.3).
 Can anyone help me, how I can perform a post hoc test (Bonferroni) on my
 data?

 thank you
 a_wohl



 --
 View this message in context:
 http://r.789695.n4.nabble.com/nested-model-and-post-hoc-tp4710784.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org javascript:; mailing list -- To UNSUBSCRIBE and
 more, see
 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.



-- 
Bert Gunter

Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom.
   -- Clifford Stoll

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested model and post hoc?

2015-08-05 Thread a_wohl
maybe i have to make my data more clear to you „smile“-Emoticon
I am working with stickleback and a parasite. for my experiments I infected
the fish, so I have 3 groups in my infected factor (infected, exposed but
not infected and naive controll)
To get some information about different time points I dissected them
1,2,4,8,16,32 days post exposure (=dpi). I had 6 dissection days on which I
dissected samples of three different dpi (for example: one day one I
dissected 1,8 and 16 dpi, on day 2 8,4,32 dpi ...)
I am sure, that my problems with the post hoc come fromthe fact, that I
didn't dissect every dpi on every dissection day. But I don't know to
account for that, besides nesting



--
View this message in context: 
http://r.789695.n4.nabble.com/nested-model-and-post-hoc-tp4710784p4710789.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] nested for loops too slow

2015-04-12 Thread Bert Gunter
Well, sort of...

aggregate() is basically a wrapper for lapply(), which ultimately must loop
over the function call at the R interpreter level, as opposed to vectorized
functions that loop at the C level and hence can be orders of magnitude
faster. As a result, there is often little difference in efficiency between
explicit and *smart* (in the sense that Pat Burns has already pointed out
of not growing structures at each iteration,among other things)  for()
looping and apply-type calls. For some of us, the chief advantage of the
*apply idioms is that the code is more readable and maintainable, with R
handling fussy details of loop indexing, for example.lapply() is also more
in keeping with the functional programming paradigm.
​Others​
find both these virtues to be annoyances,
​ ​
however, and prefer explicit *smart* looping. Chaque un á
​ ​
son goû
​t​
.

None of which necessarily denies the wisdom of the approach you've
suggested, however. It may indeed be considerably faster,
but timing will have to tell. I am just trying to correct
​(again) ​
the
​ ​
widely held misperception
​that yo​
u
​ seem to​
express
​.​


Cheers,
Bert


Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom.
Clifford Stoll



On Sun, Apr 12, 2015 at 1:48 PM, Thierry Onkelinx thierry.onkel...@inbo.be
wrote:

 You don't need loops at all.

 grw - aggregate(gw ~ ts + ISEG + iter, data = dat, FUN = sum)
 GRW - aggregate(gw ~ ts + ISEG, data = grw, FUN = function(x){max(x) -
 min(x)})
 DC - aggregate(div ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN =
 function(x){max(x) - min(x)})
 iter - aggregate(iter ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN
 = max)
 tmp - merge(DC, iter)
 merge(tmp, GRW)

 another option is to use the plyr package

 library(plyr)
 merge(
   ddply(
 subset(dat, IRCH == 1),
 c(ts, ISEG),
 summarize,
 divChng = max(div) - min(div),
 max.iter = max(iter)
   ),
   ddply(
 dat,
 c(ts, ISEG),
 summarize,
 gwChng = diff(range(ave(gw, iter, FUN = sum)))
   )
 )

 Best regards,

 ir. Thierry Onkelinx
 Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
 Forest
 team Biometrie  Kwaliteitszorg / team Biometrics  Quality Assurance
 Kliniekstraat 25
 1070 Anderlecht
 Belgium

 To call in the statistician after the experiment is done may be no more
 than asking him to perform a post-mortem examination: he may be able to say
 what the experiment died of. ~ Sir Ronald Aylmer Fisher
 The plural of anecdote is not data. ~ Roger Brinner
 The combination of some data and an aching desire for an answer does not
 ensure that a reasonable answer can be extracted from a given body of data.
 ~ John Tukey

 2015-04-12 15:47 GMT+02:00 Morway, Eric emor...@usgs.gov:

  The small example below works lighting-fast; however, when I run the same
  script on my real problem, a 1Gb text file, the for loops have been
 running
  for over 24 hrs and I have no idea if the processing is 10% done or 90%
  done.  I have not been able to figure out a betteR way to code up the
  material within the for loops at the end of the example below.  The
  contents of divChng, the final product, are exactly what I'm after, but I
  need help formulating more efficient R script, I've got two more 1Gb
 files
  to process after the current one finishes, whenever that is...
 
  I appreciate any insights/solutions, Eric
 
  dat - read.table(textConnection(ISEG  IRCH  div  gw
  1  1  265  229
  1  2  260  298
  1  3  234  196
  54  1  432  485
  54  39  467  485
  54  40  468  468
  54  41  460  381
  54  42  489  502
  1  1  265  317
  1  2  276  225
  1  3  217  164
  54  1  430  489
  54  39  456  495
  54  40  507  607
  54  41  483  424
  54  42  457  404
  1  1  265  278
  1  2  287  370
  1  3  224  274
  54  1  412  585
  54  39  473  532
  54  40  502  595
  54  41  497  441
  54  42  447  467
  1  1  230  258
  1  2  251  152
  1  3  199  179
  54  1  412  415
  54  39  439  538
  54  40  474  486
  54  41  477  484
  54  42  413  346
  1  1  230  171
  1  2  262  171
  1  3  217  263
  54  1  432  485
  54  39  455  482
  54  40  493  419
  54  41  489  536
  54  42  431  504
  1  1  1002  1090
  1  2  1222  1178
  1  3  1198  1177
  54  1  1432  1485
  54  39  1876  1975
  54  40  1565  1646
  54  41  1455  1451
  54  42  1427  1524
  1  1  1002  968
  1  2  1246  1306
  1  3  1153  1158
  54  1  1532  1585
  54  39  1790  1889
  54  40  1490  1461
  54  41  1518  1536
  54  42  1486  1585
  1  1  1002  1081
  1  2  1229  1262
  1  3  1142  1241
  54  1  1632  1659
  54  39  1797  1730
  54  40  1517  1466
  54  41  1527  1589
  54  42  1514  1612),header=TRUE)
 
  dat$seq - ifelse(dat$ISEG==1  dat$IRCH==1, 1, 0)
  tmp - diff(dat[dat$seq==1,]$div)!=0
  dat$idx - 0
  dat[dat$seq==1,][c(TRUE,tmp),]$idx - 1
  

Re: [R] nested for loops too slow

2015-04-12 Thread J Robertson-Burns

You are certainly in Circle 2 of 'The R Inferno',
which I suspect is where almost all of the
computation time is coming from.

Instead of doing:

divChng - rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter)))

it would be much better to create 'divChng' to
be the final length and then in the loop do:

divChng[count,] - c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter))
count - count + 1

You may also want to explore 'tapply'.
If this is something you will be doing
a lot of, then you should probably learn
the 'data.table' package.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat

On 12/04/2015 14:47, Morway, Eric wrote:

The small example below works lighting-fast; however, when I run the same
script on my real problem, a 1Gb text file, the for loops have been running
for over 24 hrs and I have no idea if the processing is 10% done or 90%
done.  I have not been able to figure out a betteR way to code up the
material within the for loops at the end of the example below.  The
contents of divChng, the final product, are exactly what I'm after, but I
need help formulating more efficient R script, I've got two more 1Gb files
to process after the current one finishes, whenever that is...

I appreciate any insights/solutions, Eric

dat - read.table(textConnection(ISEG  IRCH  div  gw
1  1  265  229
1  2  260  298
1  3  234  196
54  1  432  485
54  39  467  485
54  40  468  468
54  41  460  381
54  42  489  502
1  1  265  317
1  2  276  225
1  3  217  164
54  1  430  489
54  39  456  495
54  40  507  607
54  41  483  424
54  42  457  404
1  1  265  278
1  2  287  370
1  3  224  274
54  1  412  585
54  39  473  532
54  40  502  595
54  41  497  441
54  42  447  467
1  1  230  258
1  2  251  152
1  3  199  179
54  1  412  415
54  39  439  538
54  40  474  486
54  41  477  484
54  42  413  346
1  1  230  171
1  2  262  171
1  3  217  263
54  1  432  485
54  39  455  482
54  40  493  419
54  41  489  536
54  42  431  504
1  1  1002  1090
1  2  1222  1178
1  3  1198  1177
54  1  1432  1485
54  39  1876  1975
54  40  1565  1646
54  41  1455  1451
54  42  1427  1524
1  1  1002  968
1  2  1246  1306
1  3  1153  1158
54  1  1532  1585
54  39  1790  1889
54  40  1490  1461
54  41  1518  1536
54  42  1486  1585
1  1  1002  1081
1  2  1229  1262
1  3  1142  1241
54  1  1632  1659
54  39  1797  1730
54  40  1517  1466
54  41  1527  1589
54  42  1514  1612),header=TRUE)

dat$seq - ifelse(dat$ISEG==1  dat$IRCH==1, 1, 0)
tmp - diff(dat[dat$seq==1,]$div)!=0
dat$idx - 0
dat[dat$seq==1,][c(TRUE,tmp),]$idx - 1
dat$ts - cumsum(dat$idx)
dat$iter - ave(dat$seq, dat$ts,FUN=cumsum)
dat$ct - seq(1:length(dat[,1]))

timeStep - unique(dat$ts)
SEG - unique(dat$ISEG)
divChng - data.frame(ts=NA, ISEG=NA, divChng=NA, gwChng=NA, iter=NA)

#Can the following be rescripted for better harnessing R's processing power?

for (i in 1:length(timeStep)){
   for (j in 1:length(SEG)){
 datTS - subset(dat,ts==timeStep[i]  ISEG==SEG[j]  IRCH==1)
 datGW - subset(dat,ts==timeStep[i]  ISEG==SEG[j])
 grw - aggregate(gw ~ iter, datGW, sum)

 DC - max(datTS$div)-min(datTS$div)
 GRW - max(grw$gw) - min(grw$gw)
 divChng - rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter)))
   }
}
divChng - divChng[!is.na(divChng$ISEG),]

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested for loops too slow

2015-04-12 Thread Thierry Onkelinx
You don't need loops at all.

grw - aggregate(gw ~ ts + ISEG + iter, data = dat, FUN = sum)
GRW - aggregate(gw ~ ts + ISEG, data = grw, FUN = function(x){max(x) -
min(x)})
DC - aggregate(div ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN =
function(x){max(x) - min(x)})
iter - aggregate(iter ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN
= max)
tmp - merge(DC, iter)
merge(tmp, GRW)

another option is to use the plyr package

library(plyr)
merge(
  ddply(
subset(dat, IRCH == 1),
c(ts, ISEG),
summarize,
divChng = max(div) - min(div),
max.iter = max(iter)
  ),
  ddply(
dat,
c(ts, ISEG),
summarize,
gwChng = diff(range(ave(gw, iter, FUN = sum)))
  )
)

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie  Kwaliteitszorg / team Biometrics  Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-04-12 15:47 GMT+02:00 Morway, Eric emor...@usgs.gov:

 The small example below works lighting-fast; however, when I run the same
 script on my real problem, a 1Gb text file, the for loops have been running
 for over 24 hrs and I have no idea if the processing is 10% done or 90%
 done.  I have not been able to figure out a betteR way to code up the
 material within the for loops at the end of the example below.  The
 contents of divChng, the final product, are exactly what I'm after, but I
 need help formulating more efficient R script, I've got two more 1Gb files
 to process after the current one finishes, whenever that is...

 I appreciate any insights/solutions, Eric

 dat - read.table(textConnection(ISEG  IRCH  div  gw
 1  1  265  229
 1  2  260  298
 1  3  234  196
 54  1  432  485
 54  39  467  485
 54  40  468  468
 54  41  460  381
 54  42  489  502
 1  1  265  317
 1  2  276  225
 1  3  217  164
 54  1  430  489
 54  39  456  495
 54  40  507  607
 54  41  483  424
 54  42  457  404
 1  1  265  278
 1  2  287  370
 1  3  224  274
 54  1  412  585
 54  39  473  532
 54  40  502  595
 54  41  497  441
 54  42  447  467
 1  1  230  258
 1  2  251  152
 1  3  199  179
 54  1  412  415
 54  39  439  538
 54  40  474  486
 54  41  477  484
 54  42  413  346
 1  1  230  171
 1  2  262  171
 1  3  217  263
 54  1  432  485
 54  39  455  482
 54  40  493  419
 54  41  489  536
 54  42  431  504
 1  1  1002  1090
 1  2  1222  1178
 1  3  1198  1177
 54  1  1432  1485
 54  39  1876  1975
 54  40  1565  1646
 54  41  1455  1451
 54  42  1427  1524
 1  1  1002  968
 1  2  1246  1306
 1  3  1153  1158
 54  1  1532  1585
 54  39  1790  1889
 54  40  1490  1461
 54  41  1518  1536
 54  42  1486  1585
 1  1  1002  1081
 1  2  1229  1262
 1  3  1142  1241
 54  1  1632  1659
 54  39  1797  1730
 54  40  1517  1466
 54  41  1527  1589
 54  42  1514  1612),header=TRUE)

 dat$seq - ifelse(dat$ISEG==1  dat$IRCH==1, 1, 0)
 tmp - diff(dat[dat$seq==1,]$div)!=0
 dat$idx - 0
 dat[dat$seq==1,][c(TRUE,tmp),]$idx - 1
 dat$ts - cumsum(dat$idx)
 dat$iter - ave(dat$seq, dat$ts,FUN=cumsum)
 dat$ct - seq(1:length(dat[,1]))

 timeStep - unique(dat$ts)
 SEG - unique(dat$ISEG)
 divChng - data.frame(ts=NA, ISEG=NA, divChng=NA, gwChng=NA, iter=NA)

 #Can the following be rescripted for better harnessing R's processing
 power?

 for (i in 1:length(timeStep)){
   for (j in 1:length(SEG)){
 datTS - subset(dat,ts==timeStep[i]  ISEG==SEG[j]  IRCH==1)
 datGW - subset(dat,ts==timeStep[i]  ISEG==SEG[j])
 grw - aggregate(gw ~ iter, datGW, sum)

 DC - max(datTS$div)-min(datTS$div)
 GRW - max(grw$gw) - min(grw$gw)
 divChng - rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
 max(datTS$iter)))
   }
 }
 divChng - divChng[!is.na(divChng$ISEG),]

 [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested design

2014-05-15 Thread S Ellison
 I would like to apply a nested ANOVA on my dataset, but I cannot find a
 satisfying answer on how to implement it in R.

The simplest formula for a two-level nested design is resp~fac1/fac2

All of the models you list are equivalent to this after allowing for the fact 
that R drops duplicated model terms. For example, fac1+fac1/fac2 expands to 
fac1 + fac1 + fac1:fac2 ... which reduces to fac1+fac1:fac2.

All this is specified in some detail in ?formula.

S Ellison




***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
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.


Re: [R] Nested boxplot groups

2014-03-29 Thread el_alisio
Hi,

a reproducible example would help us to help you. In any case, you might
want to consider a for-loop in combination with par(mfrow = c(?, ?)). The
lattice or ggplot2 package might also provide a solution to your problem.

Cheers,

Jannes



--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-boxplot-groups-tp4687818p4687820.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Nested foreach loops in R repeating items

2014-02-05 Thread arun
Hi,
Try ?duplicated()
 apply(x,2,function(x) {x[duplicated(x)]-;x})
A.K.



Hi all, 

I have a dataset of around a thousand column and a few thousands
 of rows. I'm trying to get all the possible combinations (without 
repetition) of the data columns and process them in parallel. Here's a 
simplification of what my data and my code looks like: 

mydata - structure(list(col1 = c(231L, 8946L, 534L), col2 = c(123L, 2361L, 
65L), col3 = c(5645L, 45L, 51L), col4 = c(654L, 356L, 32L), col5 = c(21L, 
1L, 51L), col6 = c(4L, 4515L, 15L), col7 = c(6L, 1L, 535L), col8 = c(894L, 
20L, 35L), col9 = c(68L, 21L, 123L), col10 = c(46L, 2L, 2L)), .Names = 
c(col1, 
col2, col3, col4, col5, col6, col7, col8, col9, 
col10), class = data.frame, row.names = c(NA, -3L)) 

require(foreach) 

x - 
foreach(m=1:5, .combine='cbind') %:% 
foreach(j=(m+1):10, .combine='c') %do% { 
paste(colnames(mydata)[m], colnames(mydata)[j]) 

} 

x 



if you execute the command above in R, you will get this result. 



      result.1     result.2     result.3     result.4     result.5     
 [1,] col1 col2  col2 col3  col3 col4  col4 col5  col5 col6 
 [2,] col1 col3  col2 col4  col3 col5  col4 col6  col5 col7 
 [3,] col1 col4  col2 col5  col3 col6  col4 col7  col5 col8 
 [4,] col1 col5  col2 col6  col3 col7  col4 col8  col5 col9 
 [5,] col1 col6  col2 col7  col3 col8  col4 col9  col5 col10 
 [6,] col1 col7  col2 col8  col3 col9  col4 col10 col5 col6 
 [7,] col1 col8  col2 col9  col3 col10 col4 col5  col5 col7 
 [8,] col1 col9  col2 col10 col3 col4  col4 col6  col5 col8 
 [9,] col1 col10 col2 col3  col3 col5  col4 col7  col5 col9 

notice that first problem I face that in the last row of the 
second column of the  x matrix says col2 col3 which is a repetition 
of the first item (which happens also in all succeeding columns). I was 
planning to have unique combinations of all columns, which obviously, 
did not work. 

Can somebody please help me with this? My desired output would be 



      result.1     result.2     result.3     result.4     result.5     
 [1,] col1 col2  col2 col3  col3 col4  col4 col5  col5 col6 
 [2,] col1 col3  col2 col4  col3 col5  col4 col6  col5 col7 
 [3,] col1 col4  col2 col5  col3 col6  col4 col7  col5 col8 
 [4,] col1 col5  col2 col6  col3 col7  col4 col8  col5 col9 
 [5,] col1 col6  col2 col7  col3 col8  col4 col9   
 [6,] col1 col7  col2 col8  col3 col9   
 [7,] col1 col8  col2 col9   
 [8,] col1 col9  col2 col10 
 [9,] col1 col10 


Many thanks

__
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.


Re: [R] Nested foreach loops in R repeating items

2014-02-05 Thread Bert Gunter
I don't think you answered the OP's query, although I confess that I
am not so sure I understand it either (see below). In any case, I
believe the R level loop (i.e. apply()) is unnecessary. There is a
unique (and a duplicated()) method for data frames, so simply

unique(x)

returns a data frame with all the unique rows of x.

However, I don't think that's what the OP wanted. (S)he appeared to
want all unique combinations of 2 columns. If I got that right (??),
combn(ncol(x),2) gives that and could be used for indexing. I'm not
sure parallel processing is useful here, but then again, I may have
misunderstood the query. If so, my apologies, and feel free to ignore
all the above :-(  .


Cheers,
Bert




Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom.
H. Gilbert Welch




On Wed, Feb 5, 2014 at 3:26 PM, arun smartpink...@yahoo.com wrote:
 Hi,
 Try ?duplicated()
  apply(x,2,function(x) {x[duplicated(x)]-;x})
 A.K.



 Hi all,

 I have a dataset of around a thousand column and a few thousands
  of rows. I'm trying to get all the possible combinations (without
 repetition) of the data columns and process them in parallel. Here's a
 simplification of what my data and my code looks like:

 mydata - structure(list(col1 = c(231L, 8946L, 534L), col2 = c(123L, 2361L,
 65L), col3 = c(5645L, 45L, 51L), col4 = c(654L, 356L, 32L), col5 = c(21L,
 1L, 51L), col6 = c(4L, 4515L, 15L), col7 = c(6L, 1L, 535L), col8 = c(894L,
 20L, 35L), col9 = c(68L, 21L, 123L), col10 = c(46L, 2L, 2L)), .Names = 
 c(col1,
 col2, col3, col4, col5, col6, col7, col8, col9,
 col10), class = data.frame, row.names = c(NA, -3L))

 require(foreach)

 x -
 foreach(m=1:5, .combine='cbind') %:%
 foreach(j=(m+1):10, .combine='c') %do% {
 paste(colnames(mydata)[m], colnames(mydata)[j])

 }

 x



 if you execute the command above in R, you will get this result.



   result.1 result.2 result.3 result.4 result.5
  [1,] col1 col2  col2 col3  col3 col4  col4 col5  col5 col6
  [2,] col1 col3  col2 col4  col3 col5  col4 col6  col5 col7
  [3,] col1 col4  col2 col5  col3 col6  col4 col7  col5 col8
  [4,] col1 col5  col2 col6  col3 col7  col4 col8  col5 col9
  [5,] col1 col6  col2 col7  col3 col8  col4 col9  col5 col10
  [6,] col1 col7  col2 col8  col3 col9  col4 col10 col5 col6
  [7,] col1 col8  col2 col9  col3 col10 col4 col5  col5 col7
  [8,] col1 col9  col2 col10 col3 col4  col4 col6  col5 col8
  [9,] col1 col10 col2 col3  col3 col5  col4 col7  col5 col9

 notice that first problem I face that in the last row of the
 second column of the  x matrix says col2 col3 which is a repetition
 of the first item (which happens also in all succeeding columns). I was
 planning to have unique combinations of all columns, which obviously,
 did not work.

 Can somebody please help me with this? My desired output would be



   result.1 result.2 result.3 result.4 result.5
  [1,] col1 col2  col2 col3  col3 col4  col4 col5  col5 col6
  [2,] col1 col3  col2 col4  col3 col5  col4 col6  col5 col7
  [3,] col1 col4  col2 col5  col3 col6  col4 col7  col5 col8
  [4,] col1 col5  col2 col6  col3 col7  col4 col8  col5 col9
  [5,] col1 col6  col2 col7  col3 col8  col4 col9
  [6,] col1 col7  col2 col8  col3 col9
  [7,] col1 col8  col2 col9
  [8,] col1 col9  col2 col10
  [9,] col1 col10


 Many thanks

 __
 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.

__
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.


Re: [R] Nested for loop help please.

2014-01-12 Thread arun


Hi Mathew,

You must have noticed that I used:
output -vector() # instead of output=array(NA,c(length(pick.a), 
length(pick.d)))

The below methods should give the results:
Method1:
res - t(sapply(pick.a,function(x) sapply(pick.d,function(y) {ts1 - 
sum(t.sham(m.sham,x,y)); tc1 - sum(t.control(m.control,x)); ts1/tc1})))
dim(res)
#[1] 200 100

Method2:### Creating a matrix of NAs
output - array(NA,c(length(pick.a),length(pick.d)))
 for(count in 1:length(pick.a)){
 for(count1 in 1:length(pick.d)){
 ts1= sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
 tc1=sum(t.control(m.control,pick.a[count]))
 output[count,count1] - ts1/tc1
 }
 }
dim(output)
#[1] 200 100

identical(res,output)
#[1] TRUE


###Method 3:
output1 -vector() #instead of defining it as matrix
 for(count in 1:length(pick.a)){
  for(count1 in 1:length(pick.d)){
  ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
 tc1 - sum(t.control(m.control,pick.a[count]))
 output1= c(output1,ts1/tc1)
  }
  }
 m1 - matrix(output1,length(pick.a),length(pick.d),byrow=TRUE)
 identical(m1,res)
#[1] TRUE
 
A.K.




On Sunday, January 12, 2014 12:49 PM, Mathew Nagendran 
mathew.nagend...@mail.utoronto.ca wrote:
Hi Arun, thank you for all your help. Considering I only started using R a few 
months ago, your help is very valuable to me.

I have taken the steps required to make my nested for loop work. The only 
problem I face now is trying to get my matrix to fill with outputs rather than 
NA. Is there something I can do to resolve this?

m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) 
m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) 

t.control=function(m, a){(1-exp(-a*m))} 
t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}

t.ratio=function(ts, tc){(ts/tc)} 

pick.a=seq(0.01,2,by=0.01) #set of a values defined
pick.d=seq(0.01,1,by=0.01) #set of d values defined

output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
columns.

for(count in 1:length(pick.a)){
    for(count1 in 1:length(pick.d)){
        ts1=sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
        tc1=sum(t.control(m.control,pick.a[count]))
        output=c(output,ts1/tc1)
    }
}
m1=matrix(output,200,100)
print(m1)



From: arun smartpink...@yahoo.com
Sent: Saturday, January 11, 2014 6:00 PM
To: Mathew Nagendran
Subject: Re: [R] Nested for loop help please.

Hi,
I guess you need to replace t() with t.control() or t.sham()
output -vector()
for(count in 1:length(pick.a)){
for(count1 in 1:length(pick.d)){
ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1 - sum(t.control(m.control,pick.a[count]))
output= c(output,ts1/tc1)
}
}
m1 - matrix(output,200,100)
dim(m1)
#[1] 200 100
A.K.




On Saturday, January 11, 2014 3:20 PM, Mathew Nagendran 
mathew.nagend...@mail.utoronto.ca wrote:
Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?

 m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
 m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)

 t.control=function(m, a){(1-exp(-a*m))}
 t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}

 t.ratio=function(ts, tc){(ts/tc)}

 pick.a=seq(0.01,2,by=0.01) #set of a values defined
 pick.d=seq(0.01,1,by=0.01) #set of d values defined

 output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
 ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
 columns.

 for(count in 1:length(pick.a)){
+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
  unused argument(s) (pick.a[count], pick.d[count])

    [[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.

__
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.


Re: [R] Nested for loop help please.

2014-01-11 Thread Rui Barradas

Hello,

You are using the function ?t(), matrix transpose, with more than one 
argument. What is that supposed to do? The error message says that the 
other two arguments are not used (because they are illegal). And you 
don't need to transpose a vector to sum its components.


Hope this helps,

Rui Barradas

Em 11-01-2014 18:46, Mathew Nagendran escreveu:

Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?


m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)

t.control=function(m, a){(1-exp(-a*m))}
t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}

t.ratio=function(ts, tc){(ts/tc)}

pick.a=seq(0.01,2,by=0.01) #set of a values defined
pick.d=seq(0.01,1,by=0.01) #set of d values defined

output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
columns.

for(count in 1:length(pick.a)){

+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
   unused argument(s) (pick.a[count], pick.d[count])

[[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.



__
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.


Re: [R] Nested for loop help please.

2014-01-11 Thread William Dunlap
  for(count in 1:length(pick.a)){
 + for(count in 1:length(pick.d)){
 + ts=sum(t(m.sham,pick.a[count],pick.d[count]))

The variable 'count' in the inner loop is overwriting the value
of 'count' set in the outer loop.  Use different names in
the different loops.
   for(count.a in seq_along(pick.a)){
   for(count.d in seq_along(pick.d)){
   FUN(pick.a[count.a], pick.d[count.d])

(I wrote 'FUN' because the t() function is misused in the example.)

Bill Dunlap
TIBCO Software
wdunlap tibco.com


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf
 Of Mathew Nagendran
 Sent: Saturday, January 11, 2014 10:46 AM
 To: r-help@r-project.org
 Subject: [R] Nested for loop help please.
 
 Hi all I am relatively new to R. I am trying to create a nested for loop but 
 i keep getting
 an error message (unused argument). Can someone help me find out where I am 
 goign
 wrong?
 
  m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
  m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
 
  t.control=function(m, a){(1-exp(-a*m))}
  t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
 
  t.ratio=function(ts, tc){(ts/tc)}
 
  pick.a=seq(0.01,2,by=0.01) #set of a values defined
  pick.d=seq(0.01,1,by=0.01) #set of d values defined
 
  output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
  ratios. a
 values (0.01-2) in column 1 and d values (0.01-1) in the other columns.
 
  for(count in 1:length(pick.a)){
 + for(count in 1:length(pick.d)){
 + ts=sum(t(m.sham,pick.a[count],pick.d[count]))
 + tc=sum(t(m.control,pick.a[count]))
 + output[count,2]= (ts/tc)
 + }
 + print(output)
 + }
 Error in t(m.sham, pick.a[count], pick.d[count]) :
   unused argument(s) (pick.a[count], pick.d[count])
 
   [[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.

__
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.


Re: [R] Nested for loop help please.

2014-01-11 Thread arun




Hi,
I guess you need to replace t() with t.control() or t.sham()
output -vector()
for(count in 1:length(pick.a)){
 for(count1 in 1:length(pick.d)){
 ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1 - sum(t.control(m.control,pick.a[count]))
output= c(output,ts1/tc1)
 }
 }
m1 - matrix(output,200,100)
 dim(m1)
#[1] 200 100
A.K.





On Saturday, January 11, 2014 3:20 PM, Mathew Nagendran 
mathew.nagend...@mail.utoronto.ca wrote:
Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?

 m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
 m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)

 t.control=function(m, a){(1-exp(-a*m))}
 t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}

 t.ratio=function(ts, tc){(ts/tc)}

 pick.a=seq(0.01,2,by=0.01) #set of a values defined
 pick.d=seq(0.01,1,by=0.01) #set of d values defined

 output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
 ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
 columns.

 for(count in 1:length(pick.a)){
+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
  unused argument(s) (pick.a[count], pick.d[count])

    [[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.


__
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.


Re: [R] nested 'while' loops

2013-03-25 Thread Sahana Srinivasan
Hi, sorry that got sent without the output :
Please ignore the aligning in the input, I am re-adding that here as well :
1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00012902334171612422939635201325342732312
3amt:Amet_000219315421218835254372613914212030084

Output:
1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00010.831808022254316NA0.831808022254316
0.8318080222543160.8318080222543160.8318080222543160.831808022254316
0.8318080222553840.8318080222553840.8318080222553840.831808022255384
0.8318080222553840.8318080222553840.8318080222553840.831808022255384
0.8318080222553840.8318080222553840.8318080222553840.831808022255384
0.8318080222553843amt:Amet_0002NANANANANANANANANANANANANANANANANANANANA4


On Mon, Mar 25, 2013 at 5:43 PM, Sahana Srinivasan 
sahanasrinivasan...@gmail.com wrote:

 Hi everyone,
 I'm using the following code to go over every element of a data frame (row
 wise). The problem I am facing is that the outer 'x' variable is not
 incrementing itself, thus, only one row of values is obtained, and the
 program does not proceed to the next row.

 This is the code:
  while(x=coln)
  {

while(y=rown)
{
  n-as.numeric(df[[y]][x]);
   if(n0)
   {
 lim-(n-1);
 S-100;
(...)
 }
 opdf[[y]][x]-sum;
   }
   y-y+1;
 }
   x-x+1;
 }

 Here is a sample of the input data:


  GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 422939
 6 3520 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 91421
 20 300 8





[[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.


Re: [R] nested 'while' loops

2013-03-25 Thread Berend Hasselman

On 25-03-2013, at 18:43, Sahana Srinivasan sahanasrinivasan...@gmail.com 
wrote:

 Hi everyone,
 I'm using the following code to go over every element of a data frame (row
 wise). The problem I am facing is that the outer 'x' variable is not
 incrementing itself, thus, only one row of values is obtained, and the
 program does not proceed to the next row.
 
 This is the code:
 while(x=coln)
 {
 
   while(y=rown)
   {
 n-as.numeric(df[[y]][x]);
  if(n0)
  {
lim-(n-1);
S-100;
   (...)
}
opdf[[y]][x]-sum;
  }
  y-y+1;
 }
  x-x+1;
 }
 

 Here is a sample of the input data:
 
 
 GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635
 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30
 0 8

This does not provide reproducible code or an example.
You should also count the opening { and the closing }.

Berend

__
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.


Re: [R] nested 'while' loops

2013-03-25 Thread Sahana Srinivasan
while(x=21)
 {

   while(y=rown)
   {
 n-as.numeric(df[[y]][x]);
  if(n0)
  {

while(k=lim)
{

  k-k+1;
} # while loop for k closes

opdf[[y]][x]-sum;
  } # if statement closes

  y-y+1;
} #while for y closes
   x-x+1;
} #while with x closes


Have sent the code with matching brackets after confirming with what I am
using.

[[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.


Re: [R] nested 'while' loops

2013-03-25 Thread Patrick Burns

Your immediate problem is that 'y' is
not reset to 1.

Easier code to write would be to use
'for' loops rather than 'while' loops.

Better still would be to use neither if
possible.  I suspect that you are in
Circle 3 of 'The R Inferno'.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat


On 25/03/2013 17:43, Sahana Srinivasan wrote:

Hi everyone,
I'm using the following code to go over every element of a data frame (row
wise). The problem I am facing is that the outer 'x' variable is not
incrementing itself, thus, only one row of values is obtained, and the
program does not proceed to the next row.

This is the code:
  while(x=coln)
  {

while(y=rown)
{
  n-as.numeric(df[[y]][x]);
   if(n0)
   {
 lim-(n-1);
 S-100;
(...)
 }
 opdf[[y]][x]-sum;
   }
   y-y+1;
}
   x-x+1;
}

Here is a sample of the input data:


GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635
20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30
0 8

[[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.



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')

__
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.


Re: [R] nested random factor using lme produces errors

2013-02-18 Thread Ben Bolker
melswed amelie.truchy at slu.se writes:

 
 I understand. I want to specify that drug is only a fixed factor and family
 should be the only random factor. So maybe, my R code is wrong If I
 specify random=~1|drug/family it is only because I wanted to specify that
 family is nested within drug.


 Then you want 

random=~1|drug:family

drug/family expands to drug + drug:family, so it includes both an effect of
drug and a family-within-drug term.

See http://glmm.wikidot.com/faq#modelspec , which I've just updated.

__
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.


Re: [R] nested random factor using lme produces errors

2013-02-17 Thread Ben Bolker
melswed amelie.truchy at slu.se writes:

 
 Hi,
 
 I am running a mixed-effect model with a nested-random effect. I am
 interested in gut parasites in moose. I has three different type of
 treatment that I applied to moose which are from different families. My
 response variable is gut parasites and the factors are moose families which
 is nested within treatment. My data is balanced.
 
 To answer this question, I used the lme function like this :
 model=lme(parasite~drug,random=~1|drug/family)
 
 But doing a summary on this model gives me warning message :
 In pt(-abs(tTable[, t-value]), tTable[, DF]) : NaNs produced
 
 I don't understand why ?! I noticed that the p-values are not computed and
 have NAs values for drug2 and drug3 (from the summary of this model)
 
 Moreover, in the summary, I noticed that in the random effects line I have
 standard deviation for Formula: ~1 | drug and for Formula: ~1 | family %in%
 drug. Does R consider drug as a random factor as well ?
 
 And last question, how can I know if my random factor has a significant
 effect on the gut parasites ?
 

  This belongs on r-sig-mixed-mod...@r-project.org.  Hint: it very rarely
makes sense to include a categorical predictor such as drug as both
a random and a fixed effect ... this model is overspecified.  For 
computational and philosophical reasons, it seems unwise and odd 
(respectively) to treat drug as a random effect.

  I might have more to say but will say it (perhaps) if you repost on
r-sig-mixed-models .

__
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.


Re: [R] nested random factor using lme produces errors

2013-02-17 Thread melswed
I understand. I want to specify that drug is only a fixed factor and family
should be the only random factor. So maybe, my R code is wrong If I
specify random=~1|drug/family it is only because I wanted to specify that
family is nested within drug.



--
View this message in context: 
http://r.789695.n4.nabble.com/nested-random-factor-using-lme-produces-errors-tp4658837p4658878.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Nested loop and output help

2013-02-08 Thread PIKAL Petr
Hi
You must use indexing based on loops

sim(ss[i],cc[j],oo1[k],oo2[l])

an of course assign result to some object.

result[...] - sim(ss[i],cc[j],oo1[k],oo2[l])

where ... means some suitable index either chosen from loop index or 
constructed during loop execution.
Maybe it is time to go through R intro document (chapter 2 and chapter 9).

You probably could find answer yourself quicker then waiting for me to answer 
it.

Regards
Petr


From: staysafe23 [mailto:staysaf...@gmail.com]
Sent: Thursday, February 07, 2013 9:09 PM
To: PIKAL Petr
Subject: RE: [R] Nested loop and output help


Hi Petr,

I have refined my question and would like to ask it another way if I may. Here 
are the conditions I would like to vary in my inquiry.

ss - seq(5,9,by=1) 5 6 7 8 9

cc - seq(-0.5,0.5, by=0.25) -0.50 -0.25 0.00 0.25 0.50

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

oo2 - seq(0,20, by=5) 0 5 10 15 20

Here is the loop I would like to run.

for(i in ss) {

for (j in cc) {

for (k in oo1) {

for (l in oo2) {

sim(ss,cc,oo1,oo2)





}

}

}

}

There are the results that would like to get.

sim(5,-0.5,-10,0)

sim(5,-0.5,-10,5)

sim(5,-0.5,-10,10)

sim(5,-0.5,-10,15)

sim(5,-0.5,-10,20)

. . .

. . .

. . .

sim(9,0.5,10,0)

sim(9,0.5,10,5)

sim(9,0.5,10,10)

sim(9,0.5,10,15)

sim(9,0.5,10,20)

Here is my function but I am not getting the loop to run over all the possible 
permutations of the conditions nor and I getting an output for each condition.

sim - function(ss,cc,oo1,oo2) {

lll - vector(mode = list, length = 16)

names(lll) - c(og_table, McNemar, McNemar_corr,

sd_table, Chisq, Chisq_corr,

og_data, mean_X, cut_T1, mean_X2,

mean_Y,cut_T2, mean_Y2,

dev_data, sum_D, sum_D2)

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[[og_data]] - cbind(X,Y)

dev1 - oo1

lll[[cut_T1]] - mean(X) + dev1

dev2 - oo2

lll[[cut_T2]] - mean(X) + dev1 + dev2

X2 - X-lll[[cut_T1]]

Y2 - Y-lll[[cut_T2]]

c3 - cor(X2,Y2)

D - Y2-X2

lll[[sum_D]] - sum(D)

D2 -D*D

lll[[sum_D2]] - sum(D2)

lll[[dev_data]] -cbind(X2,Y2)

a11 - ifelse( X  lll[[cut_T1]]  Y  lll[[cut_T2]], 1, 0)

a12 - ifelse( X  lll[[cut_T1]]  Y = lll[[cut_T2]], 1, 0)

a21 - ifelse( X = lll[[cut_T1]]  Y  lll[[cut_T2]], 1, 0)

a22 - ifelse( X = lll[[cut_T1]]  Y = lll[[cut_T2]], 1, 0)

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

lll[[McNemar]] -mcnemar.test(lll[[og_table]], correct=FALSE)

lll[[McNemar_corr]] -mcnemar.test(lll[[og_table]], correct=TRUE)

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

lll[[Chisq]] -chisq.test(lll[[sd_table]], correct = FALSE)

lll[[Chisq_corr]] -chisq.test(lll[[sd_table]], correct = TRUE)

lll[[mean_X]] - mean(X)

lll[[mean_Y]] - mean(Y)

lll[[mean_X2]] - mean(X2)

lll[[mean_Y2]] - mean(Y2)

lll[[T]] - sum(D)/((length(D)*sum(D2)-sum(D)*sum(D))/(length(D)-1))**(0.5)

lll[[dep_t]] - t.test(Y2, X2, alternative='two.sided', conf.level=0.95, 
paired=TRUE)

print(lll)

capture.output(print(lll), file = 
C:/Chi_Square_fix/temp_[ss]_[cc]_[oo1]_[002].txt, append = TRUE)

}
On Feb 6, 2013 3:26 PM, staysafe23 
staysaf...@gmail.commailto:staysaf...@gmail.com wrote:

Hi Petr,

Thank you again for your help. I am making a mistake since I have assigned an 
entire vector oo1 to dev1. What I errantly thought I was doing was starting 
with the value of 5 in ss, then picking the value of -0.50 in cc, then picking 
the value of -10 in oo1, then picking the value of 0 in oo2, and running the 
code that is within the inner most brackets and putting the output into an 
output file.

Then I thought the loop would move on to the next case, 5 in ss, then picking 
the value of -0.50 in cc, then picking the value of -10 in oo1, then picking 
the value of 5 in oo2. So I thought I would end up with my code being run a 
number of times that would equal the 
length(ss)*length(cc)*length(oo1)*length(oo2) .

I can't seem to get the code to run through each of these conditions. Do you 
have any idea that may help me do so?

Thank you Petr,

Thomas
On Feb 6, 2013 2:25 AM, PIKAL Petr 
petr.pi...@precheza.czmailto:petr.pi...@precheza.cz wrote:
Hi

This is an error I get with your loop code due to this line

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append = TRUE)
Error in file(file, if (append) a else w) :
  cannot open the connection
In addition: Warning messages:
1: In chisq.test(lll[[mat4]], correct = FALSE) :
  Chi-squared approximation may be incorrect
2: In chisq.test(lll[[mat4]], correct = TRUE) :
  Chi-squared approximation may be incorrect
3: In file(file, if (append) a else w) :
  cannot open file 'C:/Chi_Square_fix/temp.txt': No such file or directory


Without this line I get no error and everything is probably printed. I do not 
know function capture.output but I presume that you need initialise the file 
temp.txt before you can output to it.

Anyway those cycles are rather weird. E.g. in each cycle you assign whole 
vector oo1

Re: [R] Nested loop and output help

2013-02-06 Thread PIKAL Petr
Hi

This is an error I get with your loop code due to this line

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append = TRUE)
Error in file(file, if (append) a else w) :
  cannot open the connection
In addition: Warning messages:
1: In chisq.test(lll[[mat4]], correct = FALSE) :
  Chi-squared approximation may be incorrect
2: In chisq.test(lll[[mat4]], correct = TRUE) :
  Chi-squared approximation may be incorrect
3: In file(file, if (append) a else w) :
  cannot open file 'C:/Chi_Square_fix/temp.txt': No such file or directory


Without this line I get no error and everything is probably printed. I do not 
know function capture.output but I presume that you need initialise the file 
temp.txt before you can output to it.

Anyway those cycles are rather weird. E.g. in each cycle you assign whole 
vector oo1 to dev1.
Is it intended?

Regards
Petr


From: staysafe23 [mailto:staysaf...@gmail.com]
Sent: Friday, February 01, 2013 10:12 PM
To: PIKAL Petr
Cc: r-help@r-project.org
Subject: RE: [R] Nested loop and output help


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.pi...@precheza.czmailto:petr.pi...@precheza.cz wrote:
Hi

see inline

 -Original Message-
 From: r-help-boun...@r-project.orgmailto:r-help-boun...@r-project.org 
 [mailto:r-help-bounces@r-mailto:r-help-bounces@r-
 project.orghttp://project.org] On Behalf Of staysafe23
 Sent: Friday, February 01, 2013 1:01 AM
 To: r-help@r

Re: [R] Nested loop and output help

2013-02-02 Thread staysafe23
Dear R help moderator,

My name is Thomas and I am new here. I am wondering if my post violated
some of the rules of posting here. If so can you please let me know what I
have done so that I can repost? I reread the posting guidelines and don't
see anything that I violate. I have been reading a great deal online but
just can figure out how to run the loop correctly.

Thank you kindly,

Tom
On Feb 1, 2013 2:12 PM, staysafe23 staysaf...@gmail.com wrote:

 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.pi...@precheza.cz wrote:

 Hi

 see inline

  -Original Message-
  From: r-help-boun...@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
 

Re: [R] Nested loop and output help

2013-02-01 Thread PIKAL Petr
Hi

see inline

 -Original Message-
 From: r-help-boun...@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.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 

Re: [R] Nested loop and output help

2013-02-01 Thread staysafe23
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.pi...@precheza.cz wrote:

 Hi

 see inline

  -Original Message-
  From: r-help-boun...@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
 

Re: [R] nested, unbalanced anova

2013-01-06 Thread peter dalgaard

On Jan 6, 2013, at 04:00 , Pfeiffer, Steven wrote:

 Hello,
 For an experiment, I selected plots of land within a forest either with
 honeysuckle or without honeysuckle.  Thus, my main factor is fixed, with 2
 levels: honeysuckle present(n=11) and honeysuckle absent(n=8).
 
 Within each plot of land, I have a trenched subplot and an untrenched
 subplot.
 
 Within each subplot of every plot, I measured soil moisture.  Now I need to
 do a nested Anova to compare the soil moisture values between treatments.
 I don't really want to discard some data to make the sample sizes
 balanced.
 
 Does anyone know how to do a nested, unbalanced Anova in R?

As far as I can tell, this is still an orthogonal design, so just proceed as 
usual. You're not in real trouble unless you have plots with one of the 
subplots missing. The whole thing will boil down to an analysis of the 
within-plot differences.

Just avoid things like Type-III sums of squares (base R won't do them, but 
popular add-ons will) because they get it wrong when cell counts are unequal.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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.


Re: [R] nested, unbalanced anova

2013-01-06 Thread Spencer Graves

On 1/6/2013 12:45 AM, peter dalgaard wrote:

On Jan 6, 2013, at 04:00 , Pfeiffer, Steven wrote:


Hello,
For an experiment, I selected plots of land within a forest either with
honeysuckle or without honeysuckle.  Thus, my main factor is fixed, with 2
levels: honeysuckle present(n=11) and honeysuckle absent(n=8).

Within each plot of land, I have a trenched subplot and an untrenched
subplot.

Within each subplot of every plot, I measured soil moisture.  Now I need to
do a nested Anova to compare the soil moisture values between treatments.
I don't really want to discard some data to make the sample sizes
balanced.

Does anyone know how to do a nested, unbalanced Anova in R?

As far as I can tell, this is still an orthogonal design, so just proceed as 
usual. You're not in real trouble unless you have plots with one of the 
subplots missing. The whole thing will boil down to an analysis of the 
within-plot differences.

Just avoid things like Type-III sums of squares (base R won't do them, but 
popular add-ons will) because they get it wrong when cell counts are unequal.


  Plot is  a random effect.  Honysuckle, trenched, and moisture are 
fixed.  You may also wish to consider using either the nlme or lme4 
packages, though they may not be needed unless you have plots with one 
of the subplots missing, as Prof. Dalgaard indicated. Pinhiero and 
Bates (2000) Mixed-Effects Modeling in S and S-Plus (Springer) is the 
best book I know on the subject.  The nlme package contains script files 
with names like ch01.R containing R code to work all the examples in the 
book;  system.file('scripts', package='nlme') will give you the full 
path to where it is installed.  These are necessary, because the R 
implementation contains a very few subtle changes from what is in the 
book.  There is also an r-sig-mixed-mod...@r-project.org email list that 
may interest you.  I have not used this in years, and I would expect 
that people on this email list could help you with more current 
information on what's available.



  Hope this helps.
  Spencer Graves

__
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.


Re: [R] nested, unbalanced anova

2013-01-06 Thread peter dalgaard

On Jan 6, 2013, at 09:45 , peter dalgaard wrote:

 
 Just avoid things like Type-III sums of squares (base R won't do them, but 
 popular add-ons will) because they get it wrong when cell counts are unequal.

That might be a bit unfair. Type-III methodology has its proponents, I'm just 
not one of them. Within their own logic, I'm sure Type-III SS are computed 
correctly. It's just that this is one of the cases where you can be misled into 
thinking that the design is orthogonal so Type-III and Type-II is the same. It 
isn't.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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.


Re: [R] nested, unbalanced anova

2013-01-06 Thread John Fox
Dear Peter,

Thank you for the clarification, since one (I hope) popular add-on that
computes type-II and -III tests for repeated-measures designs is the Anova()
function in the car package. 

The type-II tests are, in my opinion, preferable, because they are maximally
powerful, e.g., for main effects when the interactions to which the main
effects are marginal are zero in the population (the situation in which a
main effect test is typically of interest), but I'd argue (not here, because
it would take more space than is reasonable in an email), that the type-III
tests test reasonably interpretable hypotheses.

Steven: A forthcoming paper in the R Journal, available as a preprint at
http://journal.r-project.org/accepted/2012-02/Fox+Friendly+Weisberg.pdf,
explains how to use the Anova() and linearHypothesis() functions in the car
package for univariate and multivariate tests in repeated-measures designs.
The paper doesn't, however, try to clarify the distinctions among type-I,
II, and III tests.

Best,
 John

---
John Fox
Senator McMaster Professor of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada



 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of peter dalgaard
 Sent: Sunday, January 06, 2013 1:56 PM
 To: peter dalgaard
 Cc: r-help@r-project.org
 Subject: Re: [R] nested, unbalanced anova
 
 
 On Jan 6, 2013, at 09:45 , peter dalgaard wrote:
 
 
  Just avoid things like Type-III sums of squares (base R won't do them,
 but popular add-ons will) because they get it wrong when cell counts are
 unequal.
 
 That might be a bit unfair. Type-III methodology has its proponents, I'm
 just not one of them. Within their own logic, I'm sure Type-III SS are
 computed correctly. It's just that this is one of the cases where you
 can be misled into thinking that the design is orthogonal so Type-III
 and Type-II is the same. It isn't.
 
 --
 Peter Dalgaard, Professor,
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Email: pd@cbs.dk  Priv: pda...@gmail.com
 
 __
 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.

__
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.


Re: [R] Nested Models

2012-07-26 Thread Rolf Turner


Homework?  Ask your prof.

cheers,

Rolf Turner

On 26/07/12 11:48, stuco wrote:

Hey, I'm an R noobie and I have been trying calculate SSEr and SSEc in order
to determine if there is sufficient evidence to include second-order terms
in my model, but I have no idea what command to use.  Any help with this
would be much appreciated.


__
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.


Re: [R] Nested Models

2012-07-26 Thread peter dalgaard

On Jul 26, 2012, at 09:07 , Rolf Turner wrote:

 
 Homework?  Ask your prof.

Not necessarily, Rolf. Project work perhaps, but could also be completely 
extracurricular (knows theory and textbook notation, now has a real data set). 
I think a few hints could be in order:

(a) Figure out how to find the SSE and the MSE in _any_ linear model. Use a 
textbook example and match things up with the R notation (hint: lm(), anova(), 
summary()). 

(b) Figure out how to specify the two models that you want to compare (hint: 
y~x+I(x^2)). Again, look for a similar textbook example and compare.

(c) Done. You can however go a little further and get R to compare the two 
models directly (revisit the help for anova()). 

-pd


cheers,
 
Rolf Turner
 
 On 26/07/12 11:48, stuco wrote:
 Hey, I'm an R noobie and I have been trying calculate SSEr and SSEc in order
 to determine if there is sufficient evidence to include second-order terms
 in my model, but I have no idea what command to use.  Any help with this
 would be much appreciated.
 
 __
 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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.


Re: [R] Nested For Loop

2012-06-29 Thread Rui Barradas

Hello,

Try, in one line and no loops, surely not nested,

apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste, 
collapse=)


Hope this helps,

Rui Barradas

Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu:

I am creating a nested for loop and following are the codes I'm using, but I
am not acheiving what I want.

I have a vector

d-151:159

I have another vector

e-e-c(apple, orange, banana)

I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana

Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]-paste(d[i],e[j],sep=)
print(x[j])
}
}

The result of the above codes is


for (i in 1:length(d))

+ { for (j in 1:length(e))
+ {
+ x[j]-paste(d[i],e[j],sep=)
+ print(x[j])
+ }
+ }
[1] 151apple
[1] 151orange
[1] 151banana





What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
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.



__
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.


Re: [R] Nested For Loop

2012-06-29 Thread Bert Gunter
... or even simpler:

levels(interaction(e,d,sep=))

... and PLEASE read An Introduction to R before further posting to learn
how to program in R rather than trying to fit what you do in other
languages to R.

Cheers,
Bert

On Fri, Jun 29, 2012 at 12:48 AM, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,

 Try, in one line and no loops, surely not nested,

 apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste,
 collapse=)

 Hope this helps,

 Rui Barradas

 Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu:

 I am creating a nested for loop and following are the codes I'm using,
 but I
 am not acheiving what I want.

 I have a vector

 d-151:159

 I have another vector

 e-e-c(apple, orange, banana)

 I need to create f as
 151apple
 151orange
 151banana
 .
 .
 159apple
 159orange
 159banana

 Here is how I wrote nested for loop...

 for (i in 1:length(d))
 { for (j in 1:length(e))
 {
 x[j]-paste(d[i],e[j],sep=)
 print(x[j])
 }
 }

 The result of the above codes is

  for (i in 1:length(d))

 + { for (j in 1:length(e))
 + {
 + x[j]-paste(d[i],e[j],sep=)
 + print(x[j])
 + }
 + }
 [1] 151apple
 [1] 151orange
 [1] 151banana




 What do I need to do this looping produce the desired result.

 Thanks
 Arun
 





 --
 View this message in context: http://r.789695.n4.nabble.com/**
 Nested-For-Loop-tp4634804.htmlhttp://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
 Sent from the R help mailing list archive at Nabble.com.

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


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




-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[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.


Re: [R] Nested For Loop

2012-06-29 Thread Bert Gunter
reversed them, Should be:

levels(interaction(d,e,sep=)

 of course.

-- Bert


On Fri, Jun 29, 2012 at 2:46 AM, Bert Gunter bgun...@gene.com wrote:

 ... or even simpler:

 levels(interaction(e,d,sep=))

 ... and PLEASE read An Introduction to R before further posting to learn
 how to program in R rather than trying to fit what you do in other
 languages to R.

 Cheers,
 Bert

 On Fri, Jun 29, 2012 at 12:48 AM, Rui Barradas ruipbarra...@sapo.ptwrote:

 Hello,

 Try, in one line and no loops, surely not nested,

 apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste,
 collapse=)

 Hope this helps,

 Rui Barradas

 Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu:

 I am creating a nested for loop and following are the codes I'm using,
 but I
 am not acheiving what I want.

 I have a vector

 d-151:159

 I have another vector

 e-e-c(apple, orange, banana)

 I need to create f as
 151apple
 151orange
 151banana
 .
 .
 159apple
 159orange
 159banana

 Here is how I wrote nested for loop...

 for (i in 1:length(d))
 { for (j in 1:length(e))
 {
 x[j]-paste(d[i],e[j],sep=)
 print(x[j])
 }
 }

 The result of the above codes is

  for (i in 1:length(d))

 + { for (j in 1:length(e))
 + {
 + x[j]-paste(d[i],e[j],sep=)
 + print(x[j])
 + }
 + }
 [1] 151apple
 [1] 151orange
 [1] 151banana




 What do I need to do this looping produce the desired result.

 Thanks
 Arun
 





 --
 View this message in context: http://r.789695.n4.nabble.com/**
 Nested-For-Loop-tp4634804.htmlhttp://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
 Sent from the R help mailing list archive at Nabble.com.

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


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




 --

 Bert Gunter
 Genentech Nonclinical Biostatistics

 Internal Contact Info:
 Phone: 467-7374
 Website:

 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm





-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[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.


Re: [R] Nested For Loop

2012-06-29 Thread David Winsemius


On Jun 28, 2012, at 5:03 PM, arun.gurubaramurugeshan wrote:

I am creating a nested for loop and following are the codes I'm  
using, but I

am not acheiving what I want.

I have a vector

d-151:159

I have another vector

e-e-c(apple, orange, banana)




I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana



I admit that I think Bert's solution is way kewler than mine, but I  
would not have thought of using interaction() to mimic the crossed  
use of rep()


paste(as.character(rep(d, each=length(e))), rep(e, times=length(d) ),  
sep=)


(Looking at the code for interaction one sees that this is how it was  
coded.)


The other R functions to remember for this sort of loop-avoidance are  
'outer' and 'expand.grid'.


 outer(d,e,paste, sep=)
 [,1]   [,2][,3]
[1,] 151apple 151orange 151banana
[2,] 152apple 152orange 152banana
[3,] 153apple 153orange 153banana
[4,] 154apple 154orange 154banana
[5,] 155apple 155orange 155banana

as.vector( outer(d, e, paste, sep=) )

--
David.



Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]-paste(d[i],e[j],sep=)
print(x[j])
}
}

The result of the above codes is


for (i in 1:length(d))

+ { for (j in 1:length(e))
+ {
+ x[j]-paste(d[i],e[j],sep=)
+ print(x[j])
+ }
+ }
[1] 151apple
[1] 151orange
[1] 151banana





What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] Nested For Loop

2012-06-29 Thread arun
Hi,

I knew that you already got many replies.

Here is my contribution.
dat1-paste0(expand.grid(d,e)$Var1,expand.grid(d,e)$Var2,sep=)

or
dat2-as.vector(rbind(paste0(d,e[1],sep=),paste0(d,e[2],sep=),paste0(d,e[3],sep=)))


#Still Bert's one line is the shortest


A.K.



- Original Message -
From: arun.gurubaramurugeshan arun.gurubaramuruges...@autozone.com
To: r-help@r-project.org
Cc: 
Sent: Thursday, June 28, 2012 5:03 PM
Subject: [R] Nested For Loop

I am creating a nested for loop and following are the codes I'm using, but I
am not acheiving what I want.

I have a vector 

d-151:159

I have another vector

e-e-c(apple, orange, banana)

I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana

Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]-paste(d[i],e[j],sep=)
print(x[j])
}
}

The result of the above codes is

 for (i in 1:length(d))
+ { for (j in 1:length(e))
+ {
+ x[j]-paste(d[i],e[j],sep=)
+ print(x[j])
+ }
+ }
[1] 151apple
[1] 151orange
[1] 151banana
 


What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
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.

__
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.


Re: [R] Nested For Loop

2012-06-29 Thread R. Michael Weylandt michael.weyla...@gmail.com
Don't (can't) use paste0 with sep='' -- that's redundant, and I'm surprised not 
an error. 

Michael

On Jun 29, 2012, at 7:18 PM, arun smartpink...@yahoo.com wrote:

 Hi,
 
 I knew that you already got many replies.
 
 Here is my contribution.
 dat1-paste0(expand.grid(d,e)$Var1,expand.grid(d,e)$Var2,sep=)
 
 or
 dat2-as.vector(rbind(paste0(d,e[1],sep=),paste0(d,e[2],sep=),paste0(d,e[3],sep=)))
 
 
 #Still Bert's one line is the shortest
 
 
 A.K.
 
 
 
 - Original Message -
 From: arun.gurubaramurugeshan arun.gurubaramuruges...@autozone.com
 To: r-help@r-project.org
 Cc: 
 Sent: Thursday, June 28, 2012 5:03 PM
 Subject: [R] Nested For Loop
 
 I am creating a nested for loop and following are the codes I'm using, but I
 am not acheiving what I want.
 
 I have a vector 
 
 d-151:159
 
 I have another vector
 
 e-e-c(apple, orange, banana)
 
 I need to create f as
 151apple
 151orange
 151banana
 .
 .
 159apple
 159orange
 159banana
 
 Here is how I wrote nested for loop...
 
 for (i in 1:length(d))
 { for (j in 1:length(e))
 {
 x[j]-paste(d[i],e[j],sep=)
 print(x[j])
 }
 }
 
 The result of the above codes is
 
 for (i in 1:length(d))
 + { for (j in 1:length(e))
 + {
 + x[j]-paste(d[i],e[j],sep=)
 + print(x[j])
 + }
 + }
 [1] 151apple
 [1] 151orange
 [1] 151banana
 
 
 
 What do I need to do this looping produce the desired result.
 
 Thanks
 Arun
 
 
 
 
 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 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.
 
 __
 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.

__
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.


Re: [R] Nested longitudinal data

2012-04-23 Thread Bert Gunter
Wrong list. Post on r-sig-mixed-models.

-- Bert

On Mon, Apr 23, 2012 at 8:46 PM, arun smartpink...@yahoo.com wrote:
 Hi,

 I have some difficulty in figuring out whether I am doing correct or not.


 A brief introduction about the work: It is a light/dark choice test
 conducted in insect larvae.  The response is binary (0- present in dark
 area, 1-present in light area) and the experiment is run for 15 min, so
 there are 15 repeated measurements per individual larva at 1 min
 intervals.  The factors which affect this study are Strain (2 levels-G
 and S),  wavelength of light (4 levels-blue, green, UV, red), and
 starting response at 0 min (two levels- animal present in dark-D or
 light-L).  This is how I think it is nested.  Strain nested inside
 Wavelength, Subject (individual) nested within strain, Starting response 
 within subject, and time within Starting response.   The data looks like this:

  Number  Wavelength     Strain             Subject        
    Startingresponse              time (min)
                 1    Red  G   
 1  L   1
                 2    Red  G   
 1  L   2
     
 ---
     
 -
   20
  Red  G 1 
 L    20
                21                      Red              G                     
  2                        D                                1
     22                      Red               G                   
  2                        D                                2

     
 ---

     
 ---

  40  Red   G  2
                    D                             20

  -
              
 

            121                     Green             G
    7                           D                             1
 122                    Green              G                    7  
                          D     2
    
 
 ---

 The model I used was:


 (fm2 -lmer(Response~Wavelength+Startingresponse+Strain+ time + 
 (time|Subject),family=binomial, data=Behavdat))

 I am not sure how to specify the nested structures within the
  model.

 Any help will be appreciated.
 Thanks.
        [[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.




-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
__
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.


Re: [R] Nested brew call yields Error in .brew.cat(26, 28) : unused argument(s) (26, 28)

2012-04-02 Thread Chris Beeley
Many thanks for this. I have a follow-up question. The output that I 
have from the nested brew call includes output like this:


NANANANANANANANAN

... then a graph or a table

... then more 
NANANANANANANANANNANANANANANANANANNANANANANANANANANNANANANANANANANAN


... etc.

It only occurs in the nested brew calls, not in the top level document, 
which is absolutely fine. There are functions defined in the top level 
file which the lower level files make use of. I assumed the problem was 
caused by my not understanding the documentation to do with nested brew 
calls; evidently this is not the case.


I have several functions within the top file, one for drawing graphs, 
one for tables, another for wordclouds, etc. They all generate this 
NANANANANANANA behaviour, I have tested by putting them in and out of 
the code.


I tried to produce a minimal self contained example containing a 
function defined in the top level file used by a file called in a nested 
brew: however this file worked fine.


I realise this isn't a lot to go on, but the functions are fairly long 
and it clearly isn't a specific issue with a particular function because 
they all do it. Has anyone else ever had this happen to them? If so did 
you find a solution (other than manually removing the NAs using a final 
piece of code, which admittedly is not too arduous).


Many thanks,
Chris Beeley,
Institute of Mental Health, UK


On 30/03/2012 02:27, Matt Shotwell wrote:

On Wed, 2012-03-28 at 11:40 +0100, Chris Beeley wrote:

I am writing several webpages using the brew package and R2HTML. I would
like to work off one script so I am using nested brew calls. The
documentation for brew states that:

NOTE: brew calls can be nested and rely on placing a function named
’.brew.cat’ in the environment in which it is passed. Each time brew is
called, a check for the existence of this function is made. If it
exists, then it is replaced with a new copy that is lexically scoped to
the current brew frame. Once the brew call is done, the function is
replaced with the previous function. The function is finally removed from
the environment once all brew calls return.

I'm afraid I can't quite figure out what it is I'm supposed to do here.
I've tried loading the brew library within the script which I pass to
brew, and I've tried defining brew cat like this:

The paragraph above describes what brew is doing behind the scenes. It's
not necessary to modify or set the .brew.cat function.

A nested (or recursive) brew call occurs when brew() is called from a
document currently being processed by brew().

To illustrate further, suppose there are two brew documents,
example-1.brew and example-2.brew, where example-1.brew contains the
following text (delimited by '''):

'''
This text is in example-1.brew.
%= brew::brew(example-2.brew) %
'''

and the example-2.brew contains

'''
This text is in example-2.brew.
%= date() -%
'''

Then from the R prompt we have:

Rbrew::brew(example-1.brew)
This text is in example-1.brew.
This text is in example-2.brew.
Thu Mar 29 20:24:52 2012


.brew.cat=function(){}

This generates the following error message:

Error in .brew.cat(26, 28) : unused argument(s) (26, 28)

I think perhaps it is more likely that I need to insert into the script
the actual content of .brew.cat, but I can't seem to get R to tell me
what it is and Googling throws up a lot of stuff about beer and not much
else (drew a blank also from RSiteSearch(Nested brew))

Any help gratefully received.

Chris Beeley
Institute of Mental Health, UK

__
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.


__
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.


Re: [R] Nested brew call yields Error in .brew.cat(26, 28) : unused argument(s) (26, 28)

2012-03-29 Thread Matt Shotwell
On Wed, 2012-03-28 at 11:40 +0100, Chris Beeley wrote:
 I am writing several webpages using the brew package and R2HTML. I would 
 like to work off one script so I am using nested brew calls. The 
 documentation for brew states that:
 
 NOTE: brew calls can be nested and rely on placing a function named 
 ’.brew.cat’ in the environment in which it is passed. Each time brew is 
 called, a check for the existence of this function is made. If it 
 exists, then it is replaced with a new copy that is lexically scoped to 
 the current brew frame. Once the brew call is done, the function is 
 replaced with the previous function. The function is finally removed from 
 the environment once all brew calls return.
 
 I'm afraid I can't quite figure out what it is I'm supposed to do here. 
 I've tried loading the brew library within the script which I pass to 
 brew, and I've tried defining brew cat like this:

The paragraph above describes what brew is doing behind the scenes. It's
not necessary to modify or set the .brew.cat function.

A nested (or recursive) brew call occurs when brew() is called from a
document currently being processed by brew().

To illustrate further, suppose there are two brew documents,
example-1.brew and example-2.brew, where example-1.brew contains the
following text (delimited by '''):

'''
This text is in example-1.brew.
%= brew::brew(example-2.brew) %
'''

and the example-2.brew contains

'''
This text is in example-2.brew.
%= date() -%
'''

Then from the R prompt we have:

Rbrew::brew(example-1.brew)
This text is in example-1.brew.
This text is in example-2.brew.
Thu Mar 29 20:24:52 2012

 .brew.cat=function(){}
 
 This generates the following error message:
 
 Error in .brew.cat(26, 28) : unused argument(s) (26, 28)
 
 I think perhaps it is more likely that I need to insert into the script 
 the actual content of .brew.cat, but I can't seem to get R to tell me 
 what it is and Googling throws up a lot of stuff about beer and not much 
 else (drew a blank also from RSiteSearch(Nested brew))
 
 Any help gratefully received.
 
 Chris Beeley
 Institute of Mental Health, UK
 
 __
 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.

-- 
Matthew S. Shotwell
Assistant Professor, Department of Biostatistics
School of Medicine, Vanderbilt University
1161 21st Ave. S2323 MCN Office CC2102L
Nashville, TN 37232-2158

__
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.


Re: [R] nested if else statements

2012-02-05 Thread David Winsemius


On Feb 5, 2012, at 5:30 PM, Philip Robinson wrote:

I have a vector of 2,1,0 I want to change to 0,1,2 respectively (the  
data

is allele dosages)

I have tried multiple nested if/else statements and looked at the ? 
if help
and cannot work out what is wrong, other people have posted code  
which is

identical and they state works.

Any help would be greatly appreciated.


A[1:20]

[1] 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0


B -  2L - A

Or did I miss something in the problem statement?





B - rep(NA,length(A))



for (i in 1:length(A)){ if(A[i]==2){B[i] - 0} else

+ if(A[i]==0){B[i] - 2} else
+ if(A[i]==1){B[i] - 1}}

Error in if (A[i] == 2) { : missing value where TRUE/FALSE needed


You should look at ?if and ?ifelse more closely.

--

David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] nested if else statements

2012-02-05 Thread David Winsemius


On Feb 5, 2012, at 5:30 PM, Philip Robinson wrote:

I have a vector of 2,1,0 I want to change to 0,1,2 respectively (the  
data

is allele dosages)

I have tried multiple nested if/else statements and looked at the ? 
if help
and cannot work out what is wrong, other people have posted code  
which is

identical and they state works.

Any help would be greatly appreciated.


A[1:20]

[1] 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0


B - rep(NA,length(A))



for (i in 1:length(A)){ if(A[i]==2){B[i] - 0} else

+ if(A[i]==0){B[i] - 2} else
+ if(A[i]==1){B[i] - 1}}

Error in if (A[i] == 2) { : missing value where TRUE/FALSE needed


I realized after I sent my earlier message that there is no there- 
there at ?if. You should instead look at ?Control or perhaps ?if


--
David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] nested if else statements

2012-02-05 Thread R. Michael Weylandt
Much easier: use ifelse (the vectorized function) instead as follows:

ifelse(A  2, ifelse(A  1, 2, 1), 0)

But you could probably just do 2 - A in this case which would be easiest.

Michael

On Sun, Feb 5, 2012 at 5:30 PM, Philip Robinson
philip.c.robin...@gmail.com wrote:
 I have a vector of 2,1,0 I want to change to 0,1,2 respectively (the data
 is allele dosages)

 I have tried multiple nested if/else statements and looked at the ?if help
 and cannot work out what is wrong, other people have posted code which is
 identical and they state works.

 Any help would be greatly appreciated.

 A[1:20]
 [1] 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0

 B - rep(NA,length(A))

 for (i in 1:length(A)){ if(A[i]==2){B[i] - 0} else
 +                         if(A[i]==0){B[i] - 2} else
 +                         if(A[i]==1){B[i] - 1}}

 Error in if (A[i] == 2) { : missing value where TRUE/FALSE needed

        [[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.

__
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.


Re: [R] nested if else statements

2012-02-05 Thread Joshua Wiley
Hi Philip,

You have gotten several good responses.  For a generalization of this,
I would suggest _not_ using nested if or ifelse statements.  They
quickly become difficult to read.

d - c(1, 1, 2, 2, 2, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0)
require(car)
dnew - recode(d, 2 = 0; 1 = 1; 0 = 2)

#  d
#  [1] 1 1 2 2 2 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0
#  dnew
#  [1] 1 1 0 0 0 2 1 2 1 2 2 2 1 1 2 1 1 1 2 2

this allows for very readable code that works basically as one would
think...2 becomes 0, 1 becomes 1, 0 becomes 2.  You can also specify
an 'else' to catch everything not included in a specific statement.
To me, the readability and simplicity is worth a lot.  Under the hood,
it is a bit more complex, and perhaps not the most efficient so I
might rethink the readability if the run time takes too long because
you are working with huge amounts of data (20 million numbers only
takes 9 seconds to recode on my old laptop so definitely a lot more
than that).

Cheers,

Josh

On Sun, Feb 5, 2012 at 2:30 PM, Philip Robinson
philip.c.robin...@gmail.com wrote:
 I have a vector of 2,1,0 I want to change to 0,1,2 respectively (the data
 is allele dosages)

 I have tried multiple nested if/else statements and looked at the ?if help
 and cannot work out what is wrong, other people have posted code which is
 identical and they state works.

 Any help would be greatly appreciated.

 A[1:20]
 [1] 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 0

 B - rep(NA,length(A))

 for (i in 1:length(A)){ if(A[i]==2){B[i] - 0} else
 +                         if(A[i]==0){B[i] - 2} else
 +                         if(A[i]==1){B[i] - 1}}

 Error in if (A[i] == 2) { : missing value where TRUE/FALSE needed

        [[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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

__
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.


Re: [R] Nested model - singularities not defined

2011-12-24 Thread David Winsemius


On Dec 24, 2011, at 12:07 PM, Kristen Mancuso wrote:


I am using a nested model in R and the lm output shows 47 not defined
because of singularities and I have no idea why.  Any help on why  
this is
happening or how to fix this problem would be very much  
appreciated.  Below

is the output I received from R.



not defined because of singularities


This question should be a FAQ if it's not already:

http://lmgtfy.com/?q=%22not+defined+because+of+singularities%22



--

David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] Nested model - singularities not defined

2011-12-24 Thread Bert Gunter
... and  John Nash's comment (regarding another issue) may also be appropriate:

...  As with many tools in this domain, for effective use they
require more knowledge than many of their users possess, and can be
dangerous because they
seem to work. 


-- Bert

On Sat, Dec 24, 2011 at 9:44 AM, David Winsemius dwinsem...@comcast.net wrote:

 On Dec 24, 2011, at 12:07 PM, Kristen Mancuso wrote:

 I am using a nested model in R and the lm output shows 47 not defined
 because of singularities and I have no idea why.  Any help on why this is
 happening or how to fix this problem would be very much appreciated.
  Below
 is the output I received from R.


 not defined because of singularities


 This question should be a FAQ if it's not already:

 http://lmgtfy.com/?q=%22not+defined+because+of+singularities%22



 --

 David Winsemius, MD
 West Hartford, CT

 __
 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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.


Re: [R] nested for loops

2011-11-06 Thread nick_pan
I sent a post yesterday  that I found out why my function didn't work. It's
ok now it works.

Thank you all.


--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3994996.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] nested for loops

2011-11-05 Thread nick_pan
Thank you , this works but I have to do it with nested for loops...

Could you suggest me a way ?





--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992324.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] nested for loops

2011-11-05 Thread R. Michael Weylandt michael.weyla...@gmail.com
Why do you need to do it with nested for loops? It is of course possible - and 
I hinted how to do it in my first email - but there's no reason as far as I can 
see to do so, particularly as a means of MLE. Sounds suspiciously like 
homework...

Michael

On Nov 4, 2011, at 10:14 PM, nick_pan nick_pa...@yahoo.gr wrote:

 Thank you , this works but I have to do it with nested for loops...
 
 Could you suggest me a way ?
 
 
 
 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992324.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 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.

__
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.


Re: [R] nested for loops

2011-11-05 Thread Carl Witthoft
If in fact this is homework, you will do yourself, your classmates, and 
possibly your teacher if you let them know that, at least in R, almost 
anything you can do in a for() loop can be done easier and faster with 
vectorization.  If you teacher can't comprehend this, get him fired.


a-c(4,6,3)
b- c( 9,4,1)
d - c(4,7,2,8)

winning.value - max(outer(a,outer(b,d,*),*))


quote
From: R. Michael Weylandt michael.weylandt_at_gmail.com
Date: Sat, 05 Nov 2011 10:21:05 -0400
Why do you need to do it with nested for loops? It is of course possible 
- and I hinted how to do it in my first email - but there's no reason as 
far as I can see to do so, particularly as a means of MLE. Sounds 
suspiciously like homework...


Michael

On Nov 4, 2011, at 10:14 PM, nick_pan nick_pan88_at_yahoo.gr wrote:

 Thank you , this works but I have to do it with nested for loops...

 Could you suggest me a way ?

--

Sent from my Cray XK6
Pendeo-navem mei anguillae plena est.

__
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.


Re: [R] nested for loops

2011-11-05 Thread David Winsemius
You need to define l as a dimensioned object , either a vector or an array, 
and then assign the value of your calculation to the correctly indexed 
location in that object. Otherwise you are just overwriting the value each 
time through the loop. Use these help pages (and review Introduction to R

?array
?vector
?[

On Nov 4, 2011, at 7:49 PM, nick_pan nick_pa...@yahoo.gr wrote:

 Hi all , I have written a code with nested for loops . 
 The aim is to estimate the maximum likelihood by creating 3 vectors with the
 same length( sequence ) 
 and then to utilize 3 for loops to make combinations among the 3 vectors ,
 which are (length)^3 in number , and find the one that maximize the
 likelihood ( maximum likelihood estimator).
 
 
 The code I created, runs but I think something goes wrong...because when I 
 change the length of the vectors but not the bounds the result is the
 same!!!
 
 I will give a simple example(irrelevant but proportional to the above) to
 make it more clear...
 
 Lets say we want to find the combination that maximize the multiplication of
 the entries of some vectors.
 
 V1-c(1,2,3)
 V2-c(5, 2 , 4)
 V3-c( 4, 3, 6)
 
 The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90
 
 If I apply the following in R , I won't take this result
 
 V1-c(1,2,3)
 V2-c(5, 2 , 4)
 V3-c( 4, 3, 6)
 
 for( i in V1){
  for( j in V2) {
 for( k in V3){
 
 l- i*j*k
 
 }
 }
 }
 l
 
 Then  l- i*j*k  is  number and not vector(of all multiplications of all
 the combinations) , and is 3*4*6 = 72.
 
 How can I fix the code?
 
 
 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 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.

__
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.


Re: [R] nested for loops

2011-11-05 Thread Bert Gunter
Carl: Almost anything you can do in a for() loop can be done easier and
faster with vectorization.-- 

That is false: while this is certainly true for a great many basic
vectorized operations, it is certainly false for most other things --
simulations are a typical example. Note that __ply type operations in base
R, plyr , and other packages are (generally) _not_vectorized; they are
disguised loops. Explicit for() loops are often just as fast or even a
bit faster, though many of us prefer what we consider the more transparent
functional style code of the __ply's.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[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.


Re: [R] nested for loops

2011-11-05 Thread nick_pan
I found the way out - it was because the borders of the vectors was close
enough thats why I had the same result while I was adding points to the
sequence. The example I gave was irrelevant but I made in order to find out
that the problem was.
Thank you all for your answers.


--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3993917.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] nested for loops

2011-11-05 Thread Jeff Newmiller
Bert, this is not helpful. Since for loops and apply functions are not 
vectorized, why are you admonishing Carl that vectorizing doesn't always speed 
up algorithms? He didn't reference apply functions as being vectorized. But you 
seem to be doing so.

I would assert that vectorizing DOES always speed up algorithms, but things 
people sometimes think are vectorizing are not really.
---
Jeff Newmiller The . . Go Live...
DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Bert Gunter gunter.ber...@gene.com wrote:

Carl: Almost anything you can do in a for() loop can be done easier and
faster with vectorization.-- 

That is false: while this is certainly true for a great many basic
vectorized operations, it is certainly false for most other things --
simulations are a typical example. Note that __ply type operations in base
R, plyr , and other packages are (generally) _not_vectorized; they are
disguised loops. Explicit for() loops are often just as fast or even a
bit faster, though many of us prefer what we consider the more transparent
functional style code of the __ply's.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[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]]

__
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.


Re: [R] nested for loops

2011-11-05 Thread R. Michael Weylandt
No idea how this relates to what you said originally but glad you got
it all worked out.

And let us all reiterate: really, don't use nested for loops...there's
a better way: promise!

Michael

On Sat, Nov 5, 2011 at 2:20 PM, nick_pan nick_pa...@yahoo.gr wrote:
 I found the way out - it was because the borders of the vectors was close
 enough thats why I had the same result while I was adding points to the
 sequence. The example I gave was irrelevant but I made in order to find out
 that the problem was.
 Thank you all for your answers.


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3993917.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.


__
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.


Re: [R] nested for loops

2011-11-04 Thread R. Michael Weylandt
Your problem is that you redefine l each time through the loops and
don't record old values; you could do so by using c() for
concatenation, but perhaps this is what you are looking for:

exp(rowSums(log(expand.grid(V1, V2, V3

Hope this helps,

Michael

On Fri, Nov 4, 2011 at 7:49 PM, nick_pan nick_pa...@yahoo.gr wrote:
 Hi all , I have written a code with nested for loops .
 The aim is to estimate the maximum likelihood by creating 3 vectors with the
 same length( sequence )
 and then to utilize 3 for loops to make combinations among the 3 vectors ,
 which are (length)^3 in number , and find the one that maximize the
 likelihood ( maximum likelihood estimator).


 The code I created, runs but I think something goes wrong...because when I
 change the length of the vectors but not the bounds the result is the
 same!!!

 I will give a simple example(irrelevant but proportional to the above) to
 make it more clear...

 Lets say we want to find the combination that maximize the multiplication of
 the entries of some vectors.

 V1-c(1,2,3)
 V2-c(5, 2 , 4)
 V3-c( 4, 3, 6)

 The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90

 If I apply the following in R , I won't take this result

 V1-c(1,2,3)
 V2-c(5, 2 , 4)
 V3-c( 4, 3, 6)

 for( i in V1){
  for( j in V2) {
     for( k in V3){

 l- i*j*k

 }
 }
 }
 l

 Then  l- i*j*k  is  number and not vector(of all multiplications of all
 the combinations) , and is 3*4*6 = 72.

 How can I fix the code?




 --
 View this message in context: 
 http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.


__
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.


Re: [R] Nested lapply? Is this allowed?

2011-11-01 Thread Uwe Ligges

For short:
1. I wonder if you cannot make the two steps one and hence only need one 
lapply anway.

2. Why not just try out?
3. If it does not work, ordinary loops may be able to.

best,
Uwe Ligges



On 01.11.2011 09:24, Alaios wrote:

Dear all,

I want for a given data set to call a funciton many time. That bring us all to 
the notion of lapply (or any other variance). My problem is that this data set 
should also be created from another lapply

As for example



DataToAnalyse- return_selected_time_interval(TimeStamps,Time[[1]], 
Time[[2]])  # Where Time[[1]] and Time[[2]] are lists
 return(lapply(do_analysis(DataToAnalyse)))


as you can see the DataToAnalyse should be also an lapply. For the list of give 
Time (Time[[1]] and Time[[2]]) should create a list DataToAnalyse.

My concern though is that the DataToAnalyse for all the Time is huge so I was 
looking for something memory efficient that can do something like that

Create Data To Analyse--feed it into do_analysis--store result into a 
list(The result is small). Repeat that sentence for all Time

How I can do something like that efficiently in R?

B.R
Alex
[[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.


__
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.


Re: [R] nested anova-R chrashing

2011-09-12 Thread Ben Bolker
joerg stephan stephanj at rhrk.uni-kl.de writes:

 
 Hi,
 
 I tried to do a nested Anova with the attached Data. My response 
 variable is survivors and I would like to know the effect of 
 (insect-egg clutch) size, position (of clutch on twig) and clone 
 (/plant genotype) on the survival of eggs (due to predation). Each plant 
 was provided with three different sizes of clutches (45,15,5) and had 
 pseudo-replications of size 15 and 5 on it.

This may not be what you wanted at all, but here is my take on
what you could try with these data.

You have binomial data with lots of zeros and ones (i.e. 0 or 100%
survival), so they are unlikely to be transformable to normality
however you try.  You *might* be able to get away with modeling
these as normally distributed and then rely on permutation tests
to get your significance levels right, but (tricky as it is) I
actually think GLMMs (Zuur et al 2009, Bolker et al 2009) are
the best way to go ...

## read in the data
x - read.table(aovmisc.dat,header=TRUE)

## take a look
summary(x)
## with(x,table(clone,as.factor(size),as.factor(position)))

## make categorical versions of size  plant variables,
##  and reconstitute the total number surviving variable
x - transform(x,
   fsize=factor(size),
   fplant=factor(plant),
   tsurv=round(survivors*size)
   )

library(ggplot2)

## trying to plot everything ...
zspace - opts(panel.margin=unit(0,lines))  ## squash panels together
theme_update(theme_bw())  ## white background

## plot all data: survival vs position, with all plants represented;
##  separate subplots for each clone
## overlay GLM fit
ggplot(x,aes(x=position,y=survivors,size=size,colour=fplant))+
   geom_point(alpha=0.5)+
  facet_wrap(~clone)+xlim(0,15)+geom_line(size=0.5,alpha=0.2)+zspace+
  geom_smooth(aes(group=1,weight=size),
  colour=black,method=glm,family=binomial)

## it looks like there is a positive effect of position, and also
##  a positive effect of size (large bubbles toward high-survival)

## there are a few outliers in 'position' -- are these typos?
subset(x,position15)


## focus on size and clone instead, suppress position:

ggplot(x,aes(x=fsize,y=survivors,colour=clone))+stat_sum(aes(size=..n..))+
  facet_grid(.~clone)+
  geom_smooth(method=glm,family=binomial,aes(weight=size,
 x=as.numeric(fsize)))
## OR
## +  stat_summary(fun.y=mean,geom=line,aes(x=as.numeric(fsize)))+zspace

library(mgcv)
ggplot(x,aes(x=position,y=survivors,colour=clone))+stat_sum(aes(size=..n..))+
  facet_grid(fsize~clone)+
  geom_smooth(method=gam,family=binomial)+xlim(0,15)+zspace

## appears to be an effect of size, and a clone*size interaction.
## possible bimodal distribution (0/1) at size=5?

with(x,table(clone,plant)) ## each plant is only in one clone 
## (explicit nesting)
library(lme4)

## fit with size*clone interaction, main effect of position
## no need to nest plant in clone because they are explicitly nested
##  (i.e. unique plant IDs)
g1 - glmer(cbind(tsurv,size)~fsize*clone+position+(1|fplant),
data=x,
family=binomial)

## doesn't LOOK like overdispersion ...
sum(residuals(g1)^2)  ##  residual df
nrow(x)-length(fixef(g1))
pchisq(778,df=537)

## ... but try fitting observation-level random effect anyway
x - transform(x,obs=seq(nrow(x)))
g2 - update(g1,.~.+(1|obs))
summary(g2)
## among-observation variance 2x among-plant variance
anova(g1,g2)  ## looks much better (10 AIC units lower / p  0.05

## 
drop1(g2,test=Chisq)
## looks like we can drop the size*clone interaction?

g3 - update(g2,.~.-clone:fsize)
drop1(g3,test=Chisq)
## strong effects of size, position;
## weak effects of clone
fixef(g3)



__
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.


Re: [R] nested loop for

2011-07-22 Thread paloma ruiz benito

Dear Dennins,

Many thanks for you reply. Sorry for the convoluted loops, but I am starting to 
learn some programming and the loops are a bit complicated for me. Finally, a 
collegue help me with the code, and one code that works is like follow:

Pma-rep (1:40)
P-seq(1,4, 1)
Plot-rep(P,10)
dbh2-rnorm(40, mean=200, sd=5)
SBA2-rnorm(40, mean=10, sd=1)

BAL2-rep(0,length(Pma))

data-data.frame(Pma,Plot,dbh2,SBA2,BAL2)

for (p in 1:length(unique(Plot))){ 
index.stand-which(data$Plot==p)  
data.aux-data[ index.stand , ] 
result.aux-numeric() 
for (i in 1:length(data.aux$Pma)){
result.aux[i] -sum(data.aux$SBA2[which(data.aux$dbh2 data.aux[i , 
]$dbh2)]) }
data$BAL2[ index.stand]-result.aux 
rm(data.aux)
rm(result.aux) 
}
data

Best,

Paloma

 Date: Thu, 21 Jul 2011 14:18:37 -0700
 Subject: Re: [R] nested loop for
 From: djmu...@gmail.com
 To: paloma_...@hotmail.com
 CC: r-help@r-project.org
 
 Hi:
 
 I *think* this is what you're after, but I get dizzy trying to read
 convoluted loops. Try this instead;
 
 # Function to find the total SBA2 for all dbh2 larger than the given one:
 foo - function(d) {
 d - d[order(d$dbh2), ]
 d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2))
 d
   }
 
 library('plyr')
 ddply(kk, .(Plot), foo)
Pma Plot dbh2  SBA2  BAL2
 1   291 185.8568  9.055821 91.530165
 2   331 186.4623 10.762347 80.767818
 311 192.8324 10.741988 70.025830
 4   171 196.2093  9.484601 60.541229
 5   211 204.0971 11.389817 49.151412
 6   131 204.5070  9.644655 39.506756
 791 205.3079 11.014892 28.491864
 8   251 206.5908 10.041878 18.449986
 951 206.8110  8.602678  9.847307
 10  371 211.1735  9.847307  0.00
 ...
 
 # If you want the groupwise sum in the output, use the following
 version instead:
 foo - function(d) {
 d - d[order(d$dbh2), ]
 d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2),
  aa = sum(SBA2))
 d
   }
 
 HTH,
 Dennis
 
 On Thu, Jul 21, 2011 at 4:13 AM, paloma ruiz benito
 paloma_...@hotmail.com wrote:
 
  Hi everyone,
 
  I have been working some days in a nested loop in R but I can't find the 
  solution.
 
  I have a data.frame with an unique ID for individuals and unique ID for 
  different stands, for each indiviadual I have a dbh record and a SBA (stand 
  basal area) field.
 
  Pma-rep (1:40)
  P-seq(1,4, 1)
  Plot-rep(P,10)
  dbh2-rnorm(40, mean=200, sd=5)
  SBA2-rnorm(40, mean=10, sd=1)
 
  As I want to calculate the basal area of larger trees in each stand (i.e., 
  the compare tree to tree the dbh and for each individual sum the stand 
  basal area of larger trees)
 
  BAL2-rep(0,length(Pma))
  aa-rep(0,length(Pma))
 
  Now I have programed a for loop to do this calculation, and one plot it 
  works quite well:
 
  kk-data.frame(Pma, Plot, dbh2, SBA2, BAL2, aa)
  kkk-kk[kk$Plot==1,]
 
 
  The loop:
 
 for(j in 1:length(kkk$Pma)){
 for(i in 1:length(kkk$Pma)){
 if(kkk$dbh2[j]kkk$dbh2[i])
 kkk$aa[i]-kkk$SBA2[i]
 else temp_data$aa[i]-0
 kkk$BAL2[j]-sum(kkk$aa)
 }
 }
 
  But, I have tried a million of forms to calculate this for each stand... 
  and no one looks fine. The closest code that I have got is:
 
  for(k in 1:length(kk)){
 temp_data-kk[kk$Plot==Plot[k],]
 
 for(j in 1:length(temp_data$Pma)){
 #I have selected the individuals to calculate the BAL in each plot
 for(i in 1:length(temp_data$Pma)){
 if(temp_data$dbh2[j]temp_data$dbh2[i])
 temp_data$aa[i]-temp_data$SBA2[i]
 else temp_data$aa[i]-0
 temp_data$BAL2[j]-sum(temp_data$aa)
 
 
 }
 
 ###Some suggestion to save the temporal data of each stand and group 
  it together
 }
 
  }
 
  Any advise or suggestion will be very welcome,
 
  Thanks in advance,
 
  Paloma
 
 
 [[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]]

__
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.


Re: [R] nested loop for

2011-07-21 Thread Dennis Murphy
Hi:

I *think* this is what you're after, but I get dizzy trying to read
convoluted loops. Try this instead;

# Function to find the total SBA2 for all dbh2 larger than the given one:
foo - function(d) {
d - d[order(d$dbh2), ]
d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2))
d
  }

library('plyr')
ddply(kk, .(Plot), foo)
   Pma Plot dbh2  SBA2  BAL2
1   291 185.8568  9.055821 91.530165
2   331 186.4623 10.762347 80.767818
311 192.8324 10.741988 70.025830
4   171 196.2093  9.484601 60.541229
5   211 204.0971 11.389817 49.151412
6   131 204.5070  9.644655 39.506756
791 205.3079 11.014892 28.491864
8   251 206.5908 10.041878 18.449986
951 206.8110  8.602678  9.847307
10  371 211.1735  9.847307  0.00
...

# If you want the groupwise sum in the output, use the following
version instead:
foo - function(d) {
d - d[order(d$dbh2), ]
d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2),
 aa = sum(SBA2))
d
  }

HTH,
Dennis

On Thu, Jul 21, 2011 at 4:13 AM, paloma ruiz benito
paloma_...@hotmail.com wrote:

 Hi everyone,

 I have been working some days in a nested loop in R but I can't find the 
 solution.

 I have a data.frame with an unique ID for individuals and unique ID for 
 different stands, for each indiviadual I have a dbh record and a SBA (stand 
 basal area) field.

 Pma-rep (1:40)
 P-seq(1,4, 1)
 Plot-rep(P,10)
 dbh2-rnorm(40, mean=200, sd=5)
 SBA2-rnorm(40, mean=10, sd=1)

 As I want to calculate the basal area of larger trees in each stand (i.e., 
 the compare tree to tree the dbh and for each individual sum the stand basal 
 area of larger trees)

 BAL2-rep(0,length(Pma))
 aa-rep(0,length(Pma))

 Now I have programed a for loop to do this calculation, and one plot it works 
 quite well:

 kk-data.frame(Pma, Plot, dbh2, SBA2, BAL2, aa)
 kkk-kk[kk$Plot==1,]


 The loop:

        for(j in 1:length(kkk$Pma)){
            for(i in 1:length(kkk$Pma)){
                if(kkk$dbh2[j]kkk$dbh2[i])
                kkk$aa[i]-kkk$SBA2[i]
                else temp_data$aa[i]-0
                kkk$BAL2[j]-sum(kkk$aa)
    }
    }

 But, I have tried a million of forms to calculate this for each stand... and 
 no one looks fine. The closest code that I have got is:

 for(k in 1:length(kk)){
    temp_data-kk[kk$Plot==Plot[k],]

        for(j in 1:length(temp_data$Pma)){
    #I have selected the individuals to calculate the BAL in each plot
            for(i in 1:length(temp_data$Pma)){
                if(temp_data$dbh2[j]temp_data$dbh2[i])
                temp_data$aa[i]-temp_data$SBA2[i]
                else temp_data$aa[i]-0
                temp_data$BAL2[j]-sum(temp_data$aa)


    }

    ###Some suggestion to save the temporal data of each stand and group it 
 together
    }

 }

 Any advise or suggestion will be very welcome,

 Thanks in advance,

 Paloma


        [[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.


__
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.


Re: [R] Nested design

2011-05-28 Thread Dennis Murphy
Hi:

Essentially, you are asking for free statistical advice, which is not
within the intended scope of R-help. It's always better to consult
with someone locally, and as luck would have it, your university
apparently provides free statistical consulting for faculty and grad
students:

http://www.uu.nl/faculty/socialsciences/EN/organisation/Departments/methodologystatistics/consultation/Pages/default.aspx

I would suggest that you contact someone there and have a face-to-face
discussion rather than a possibly extended back-and-forth on the Net.

Dennis

On Sat, May 28, 2011 at 6:09 AM, unpeatable bjorn.robr...@gmail.com wrote:
 Dear R-users,
 I have the following problem. I have performed an experiment for which I
 gathered a lot of data which I now want to test. The problem is that I
 cannot find an appropriate test in R (I am a starter) and someone might give
 me a hand. This is what I have done:
 Across three sites (Site), I have laid out five transects (Trans)...meaning
 five transects in each sites. In each transect I have five Microhabitats
 (MH) which should be regarded as subplots (I think). In each transect, every
 MH has the same position (so they are not randomized). I now want to test
 the effect of Site and MH (nested in Trans) on my response variables.

 This is what I do now: model-aov(Response~Site*MH+error(Trans/MH))

 I get the following output:

 Error: Trans
     Df   Sum Sq  Mean Sq
 Site  1 0.030294 0.030294

 Error: Trans:MH
     Df  Sum Sq Mean Sq
 Site  1 10.8367 10.8367
 MH    3  0.2836  0.0945

 Error: Within
          Df  Sum Sq Mean Sq F value    Pr(F)
 Site       2 0.92504 0.46252 11.7304 5.880e-05 ***
 MH         4 1.86688 0.46672 11.8370 5.645e-07 ***
 Site:MH    8 1.17041 0.14630  3.7105  0.001615 **
 Residuals 54 2.12917 0.03943
 ---
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 How do I read this?
 Any help appreciated!

 BTW: I also tried the lme function: model-lme(Response~Site*MH,
 random=~1|Trans/MH)
 but then the output is really complicated.


 -
 Dr. Bjorn JM Robroek
 Ecology and Biodiversity Group
 Institute of Environmental Biology, Utrecht University
 Padualaan 8, 3584 CH Utrecht, The Netherlands
 Email address: b.j.m.robr...@uu.nl
 http://www.researcherid.com/rid/C-4379-2008
 Tel: +31-(0)30-253 6091
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Nested-design-tp3557404p3557404.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.


__
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.


Re: [R] Nested design

2011-05-28 Thread unpeatable
Dear Dennis,
In my opinion I am not at all asking for any stats help, just a question how
to read this output. 
Thanks, Bjorn

-
Dr. Bjorn JM Robroek
Ecology and Biodiversity Group
Institute of Environmental Biology, Utrecht University
Padualaan 8, 3584 CH Utrecht, The Netherlands
Email address: b.j.m.robr...@uu.nl
http://www.researcherid.com/rid/C-4379-2008
Tel: +31-(0)30-253 6091
--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-design-tp3557404p3557472.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Nested design

2011-05-28 Thread Joshua Wiley
Hi,

If you are not asking for stats help, then do you understand the model
and are just confused by how R labels it?  We can help match R's
labels to the ones you are used to, if you tell us what you are used
to.

Cheers,

Josh

On Sat, May 28, 2011 at 6:54 AM, unpeatable bjorn.robr...@gmail.com wrote:
 Dear Dennis,
 In my opinion I am not at all asking for any stats help, just a question how
 to read this output.
 Thanks, Bjorn

 -
 Dr. Bjorn JM Robroek
 Ecology and Biodiversity Group
 Institute of Environmental Biology, Utrecht University
 Padualaan 8, 3584 CH Utrecht, The Netherlands
 Email address: b.j.m.robr...@uu.nl
 http://www.researcherid.com/rid/C-4379-2008
 Tel: +31-(0)30-253 6091
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Nested-design-tp3557404p3557472.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
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.


Re: [R] Nested design

2011-05-28 Thread Mike Marchywka












 Date: Sat, 28 May 2011 09:33:03 -0700
 From: jwiley.ps...@gmail.com
 To: bjorn.robr...@gmail.com
 CC: r-help@r-project.org
 Subject: Re: [R] Nested design

 Hi,

 If you are not asking for stats help, then do you understand the model
 and are just confused by how R labels it? We can help match R's
 labels to the ones you are used to, if you tell us what you are used
 to.


I would not suggest as a rule to use a tool to validate itself but
you can use R to make sure your interpretation of other R output is right by 
giving 
contrived datasets to the analysis package and see what you get back.
Comparison can be to examples from text book or your own paper and pencil
analysis.  This is also a good way to learn things from basic terms to
things like sign or unit conventions in different fields etc. 

You can generate samples from normal distro and feed that to
the questionable package to see what comes back. 



 Cheers,

 Josh

 On Sat, May 28, 2011 at 6:54 AM, unpeatable  wrote:
  Dear Dennis,
  In my opinion I am not at all asking for any stats help, just a question how
  to read this output.
  Thanks, Bjorn
 
  -
  Dr. Bjorn JM Robroek
  Ecology and Biodiversity Group
  Institute of Environmental Biology, Utrecht University
  Padualaan 8, 3584 CH Utrecht, The Netherlands
  Email address: b.j.m.robr...@uu.nl
  http://www.researcherid.com/rid/C-4379-2008
  Tel: +31-(0)30-253 6091
  --
  View this message in context: 
  http://r.789695.n4.nabble.com/Nested-design-tp3557404p3557472.html
  Sent from the R help mailing list archive at Nabble.com.
 
  __
  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.
 



 --
 Joshua Wiley
 Ph.D. Student, Health Psychology
 University of California, Los Angeles
 http://www.joshuawiley.com/

 __
 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.
  
__
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.


Re: [R] nested case-control study

2011-03-01 Thread Terry Therneau
1. Using offset(logweight) in coxph is the same as using an offset
logweight; statement in SAS, and neither is the same as case weights.

2. For a nested case control, which is what you said you have, the
strata controls who is in what risk set.  No trickery with start,stop
times is needed.  It does no harm, but is not needed.
  In a case-cohort design the start= stop-epsilon trick is one way to
set risk sets up properly.  But that's not the design you gave for your
study.  

Terry T.



On Mon, 2011-02-28 at 17:02 -0800, array chip wrote:
 Terry, thanks very much! 
 
 Professor Langholz used a SAS software trick to estimate absolute risk
 by creating a fake variable entry_time that is 0.001 less than the
 variable exit_time (i.e. time to event), and then use both variables
 in Phreg. Is this equivalent to your creating a dummy survival with
 time=1?
 
 Another question is, is using offset(logweight) inside the formula of
 coxph() the same as using weight=logweight argument in coxph(),
 because my understanding of Professor Langholz's approach for nested
 case-control study is weighted regression?
 
 Thank you very much for the help.
 
 John

__
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.


  1   2   >