Josh B
2008-Sep-27 15:10 UTC
[R] Using "by" to create individual variance-covariance matrices
Hello R list subscribers, I am trying to use the "by" command to create line-specific variance covariance matrices (where "x" is the original data matrix): by(x, x$line, function(d) { d.clean <- d[,-1]}) write.table(d.clean$line[1,1], sep = ",", file = "covariances.csv", col.names = FALSE, row.names = FALSE, append = TRUE) write.table("", sep = ",", file = "covariances.csv", col.names = FALSE, row.names = FALSE, append = TRUE) x.cov <- cov(d.clean, y= NULL, use= "complete.obs", method= c("pearson")) }) When I run this code, I get an error:> by(x, x$line, function(d) {+ d.clean <- d[,-1]}) Error in names(IND) <- deparse(substitute(INDICES))[1] : 'names' attribute [1] must be the same length as the vector [0] Any suggestions on how to improve my code? Thanks in advance. Sincerely, Josh [[alternative HTML version deleted]]
Uwe Ligges
2008-Sep-28 18:13 UTC
[R] Using "by" to create individual variance-covariance matrices
Let me repeat an important topic from the the posting guide: it would be helpful for us to see a toy example, i.e. code to generate data for x. Best wishes, Uwe Ligges Josh B wrote:> Hello R list subscribers, > > I am trying to use the "by" command to create line-specific variance covariance matrices (where "x" is the original data matrix): > > by(x, x$line, function(d) { > d.clean <- d[,-1]}) > write.table(d.clean$line[1,1], sep = ",", file = "covariances.csv", col.names = FALSE, row.names = FALSE, append = TRUE) > write.table("", sep = ",", file = "covariances.csv", col.names = FALSE, row.names = FALSE, append = TRUE) > x.cov <- cov(d.clean, y= NULL, use= "complete.obs", method= c("pearson")) > }) > > When I run this code, I get an error: > >> by(x, x$line, function(d) { > + d.clean <- d[,-1]}) > Error in names(IND) <- deparse(substitute(INDICES))[1] : > 'names' attribute [1] must be the same length as the vector [0] > > Any suggestions on how to improve my code? Thanks in advance. > > Sincerely, Josh > > > > [[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.
Possibly Parallel Threads
- Accessing the fixed- and random-effects variance-covariance matrices of an nlme model
- Looping over groups
- nlme and variance-covariance matrices.
- Calculate variance/covariance with complex numbers
- how to calculate a variance and covariance matrix for a vector