Michelle DePrenger-Levin
2008-Feb-22 23:44 UTC
[R] projection.matrix() {popbio} for more than one matrix
Hello, I am trying to use the projection.matrix( ) function and am following the example given. I have my data formatted very similar to the test.census example.> str(AsMi05mat)`data.frame': 1854 obs. of 6 variables: $ Tag : num 501 502 503 504 505 506 507 508 509 510 ... $ Year : int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ... $ Length : num 34 37 11 24 7 44 4 7 12 20 ... $ Flowering: int 1 1 0 1 0 1 0 0 0 1 ... $ Fruits : int 22 22 0 89 0 15 0 0 0 0 ... $ Stage : Factor w/ 5 levels "","Dead","Dormant",..: 4 4 5 4 5 4 5 5 5 4 ... The example data includes three years but only shows how to create a matrix from year 1 to 2. I have 13 years of data and would like to automate creating all matrices for each pair of years. I tried a for( ) loop but don't know how to assign new names for each matrix so end up with only the final comparison (2005 to 2006). I assume an apply( ) function is the way to go but can't see how to do it. Thanks for any help! Michelle [[alternative HTML version deleted]]
Uwe Ligges
2008-Feb-24 19:04 UTC
[R] projection.matrix() {popbio} for more than one matrix
Michelle DePrenger-Levin wrote:> Hello, > > > > I am trying to use the projection.matrix( ) function and am following the > example given. I have my data formatted very similar to the test.census > example. > > > >> str(AsMi05mat) > > `data.frame': 1854 obs. of 6 variables: > > $ Tag : num 501 502 503 504 505 506 507 508 509 510 ... > > $ Year : int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ... > > $ Length : num 34 37 11 24 7 44 4 7 12 20 ... > > $ Flowering: int 1 1 0 1 0 1 0 0 0 1 ... > > $ Fruits : int 22 22 0 89 0 15 0 0 0 0 ... > > $ Stage : Factor w/ 5 levels "","Dead","Dormant",..: 4 4 5 4 5 4 5 5 5 4 > ... > > > > The example data includes three years but only shows how to create a matrix > from year 1 to 2. I have 13 years of data and would like to automate creating > all matrices for each pair of years. > > > > I tried a for( ) loop but don't know how to assign new names for each matrix > so end up with only the final comparison (2005 to 2006). I assume an apply( ) > function is the way to go but can't see how to do it.Well, we cannot help to improve your code if we have neither, code nor data. Please read the posting guide and specify your code. If something is wrong in your code, we might be able to help. Uwe Ligges> > > Thanks for any help! > > > > Michelle > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Chris Stubben
2008-Feb-25 03:29 UTC
[R] projection.matrix() {popbio} for more than one matrix
Michelle, I would probably run a loop as well and save all matrices to a single list (see hudsonia and calathea on working with lists of matrices). First, run the example(test.census) to get the stage-fate data frame "trans" and then run this code to save the matrices into a list "all". years<-2001:2002 #or unique(trans$year) all<-vector("list", length(years) ) names(all)<-years ## loop through years for (s.year in years) { ## Add individual fertilities test.trans <- subset(trans, year==s.year) seedlings<-nrow(subset(test.census, year==s.year+1 & stage=="seedling")) test.trans$seedling<-test.trans$fruits/sum(test.trans$fruits) * seedlings ## save test.trans to another list if needed for bootstrapping, calculating pooled matrix etc. ## test.trans ## either one will work to get list index all[[as.character(s.year)]] <-projection.matrix(test.trans) ##all[[ s.year- (years[1]-1) ]] <-projection.matrix(test.trans) } all $`2001` seedling vegetative reproductive seedling 0.0 0.0 1.6667 vegetative 0.5 0.5 0.0000 reproductive 0.0 0.5 0.6667 $`2002` seedling vegetative reproductive seedling 0.0 0.0 0.6667 vegetative 0.2 0.5 0.3333 reproductive 0.0 0.0 0.0000 There's also a loop in the demo(fillmore) example that uses the aq.census data to create matrices for seven years. Chris Michelle DePrenger-Levin wrote:> > Hello, > > > > I am trying to use the projection.matrix( ) function and am following the > example given. I have my data formatted very similar to the test.census > example. > > > >> str(AsMi05mat) > > `data.frame': 1854 obs. of 6 variables: > > $ Tag : num 501 502 503 504 505 506 507 508 509 510 ... > > $ Year : int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ... > > $ Length : num 34 37 11 24 7 44 4 7 12 20 ... > > $ Flowering: int 1 1 0 1 0 1 0 0 0 1 ... > > $ Fruits : int 22 22 0 89 0 15 0 0 0 0 ... > > $ Stage : Factor w/ 5 levels "","Dead","Dormant",..: 4 4 5 4 5 4 5 5 5 > 4 > ... > > > > The example data includes three years but only shows how to create a > matrix > from year 1 to 2. I have 13 years of data and would like to automate > creating > all matrices for each pair of years. > > > > I tried a for( ) loop but don't know how to assign new names for each > matrix > so end up with only the final comparison (2005 to 2006). I assume an > apply( ) > function is the way to go but can't see how to do it. > > > > Thanks for any help! > > > > Michelle > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://www.nabble.com/projection.matrix%28%29-%7Bpopbio%7D-for-more-than-one-matrix-tp15646115p15673408.html Sent from the R help mailing list archive at Nabble.com.
Michelle DePrenger-Levin
2008-Feb-26 16:07 UTC
[R] projection.matrix() {popbio} for more than one matrix
Hello, I am trying to use the projection.matrix( ) function and am following the example given. I have my data formatted very similar to the test.census example.> str(AsMi05mat)`data.frame': 1854 obs. of 6 variables: $ Tag : num 501 502 503 504 505 506 507 508 509 510 ... $ Year : int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ... $ Length : num 34 37 11 24 7 44 4 7 12 20 ... $ Flowering: int 1 1 0 1 0 1 0 0 0 1 ... $ Fruits : int 22 22 0 89 0 15 0 0 0 0 ... $ Stage : Factor w/ 5 levels "","Dead","Dormant",..: 4 4 5 4 5 4 5 5 5 4 ... The example data includes three years but only shows how to create a matrix from year 1 to 2. I have 13 years of data and would like to automate creating all matrices for each pair of years. My code is the following: AsMi05mat <- read.csv("C:/AsMi05mat.csv") library(popbio) library(base) ##for(i in 1995:2005){ ## AsMitrans.i <- subset(merge(AsMi05mat, AsMi05mat, by = "Tag", sort FALSE), ## Year.x == i & Year.y == (i+1) ) ##} AsMi05trans <- subset(merge(AsMi05mat, AsMi05mat, by = "Tag", sort = FALSE), Year.x == 1995 & Year.y == 1996 ) stages <- c("Vegetative", "Reproductive", "Dead", "Dormant") projection.matrix(AsMi05trans, stage=Stage.x, fate=Stage.y, sort=stages, TF=TRUE) I tried a for( ) loop but don't know how to assign new names for each matrix so end up with only the final comparison (2005 to 2006). I assume an apply( ) function is the way to go but can't see how to do it. Thanks for any help! Michelle [[alternative HTML version deleted]]