0

The 'betadisper' funtion in vegan [R] calculates the multivariate dispersion of a group of sites based on their distances

I have a distance matrix with multiple groups of sites:

dis <- vegdist(correct_tree_data)

I created my 'groups' using the 'factor' function (23 levels for 23 groups of sites), and each group has a different no. of sites

groups <- factor(c(rep(1,144), rep(2,49), rep(3,121), rep(4,81), rep(5,81), rep(6,81), rep(7,36), rep(8,289), rep(9,324), rep(10,225), rep(11,256), rep(12,225), rep(13,289), rep(14,289), rep(15,144), rep(16,225), rep(17,225), rep(18,225), rep(19,225), rep(20,225), rep(21,225), rep(22,225), rep(23,225)), labels = c("s1_05","s2_05","s3_05","s4_05","s5_05","s6_05","s7_05","s1_10","s2_10","s3_10","s4_10","s5_10","s6_10","s7_10","s8_10","s1_15","s2_15","s3_15","s4_15","s5_15","s6_15","s7_15","s8_15"))

When using the 'betadisper' function, however, I get the following error message:

mod <- betadisper(dis, groups)

Error in pts[groups == i, , drop = FALSE] : (subscript) logical subscript too long

The levels do match the amount of groups in the distance matrix as well as the no. of replicates in each group

What else may be contributing to this error?

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • Where does `correct_tree_data` come from? Can you please make this example fully reproducible? http://stackoverflow.com/help/mcve http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jul 04 '16 at 13:35
  • Based on some experimenting I just did with these functions I'd say that the answer is that the `groups` factor length doesn't really match the `dis` object's dimensions, but it's impossible to really tell what's going on in your case without a reproducible example. – Hack-R Jul 04 '16 at 13:42
  • This would be simpler if you used just `rep(1:23, each = c(144, 49, 121, ...))` – Gavin Simpson Jul 05 '16 at 17:20

1 Answers1

0

As long as the dimensions/lengths match this function should work fine:

require(vegan)
data(varespec)

dis   <- vegdist(varespec)
group <- factor(rep("grazed", NROW(varespec)))
mod   <- betadisper(dis, group)
mod

Homogeneity of multivariate dispersions

Call: betadisper(d = dis, group = group)

No. of Positive Eigenvalues: 15 No. of Negative Eigenvalues: 8

Average distance to median: grazed 0.4255

Eigenvalues for PCoA axes: PCoA1 PCoA2 PCoA3 PCoA4 PCoA5 PCoA6 PCoA7 PCoA8 1.7552 1.1334 0.4429 0.3698 0.2454 0.1961 0.1751 0.1284

However if your factor length is longer than it should be you'll get that error:

group <- c(group, "extra data")
betadisper(dis, group)

Error in pts[groups == i, , drop = FALSE] : (subscript) logical subscript too long

I know that you don't think this is the problem you're having, so if you provide a reproducible example we can investigate your specific case further.

Community
  • 1
  • 1
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • Hi @Hack, thank you for your input. I though that rep meant the number of cells or replicates within a site. After correcting for the number of sites within a group it worked. Thanks again! – Elena Reljic Jul 05 '16 at 11:52