Re: [R-sig-eco] Community distance matrix deconstruction

2014-12-12 Thread Jari Oksanen
Kate,

Your question really may need some clarification, but at the moment it looks to 
me that you want to have row indices and column indices for your 
dissimilarities, and information about within/between dissimilarities. If this 
is what you want to have, it is an easy task.

In the following I use a real data set from vegan to make this task a bit more 
general:

library(vegan)
data(mite, mite.env)
## dissimilarities
d <- dist(mite)
## row and column indices
row <- as.dist(row(as.matrix(d)))
col <- as.dist(col(as.matrix(d)))
## within same class: 1 = within, 0 = between
within <- with(mite.env, as.dist(outer(Shrub, Shrub, "==")))
## data frame -- the pedestrian way: snappier alternatives possible 
df = data.frame(row=as.vector(row), col=as.vector(col), 
within=as.vector(within), dist=as.vector(d))
## see it
tail(df)
> tail(df)
# row col within  dist
#2410  68  67  1 691.69502
#2411  69  67  0 716.93863
#2412  70  67  0 700.60973
#2413  69  68  0  78.08329
#2414  70  68  0  24.24871
#2415  70  69  1  67.86015

I don't think you really want to have this: you only believe that you want to 
have this (mauvaise foi, like they used to say).

If you only want to get summaries, check function meandist in vegan.

Cheers, Jari Oksanen



On 13/12/2014, at 02:17 AM, Kate Boersma wrote:

> Hi all.
> 
> I have a community analysis data manipulation puzzle for you... hopefully 
> someone can help. Please let me know if this question needs clarification, 
> has previously been answered, or would be better sent to a different list.
> 
> Details follow.
> 
> Thank you,
> 
> Kate
> 
> ---
> 
> Here is a simplified version of my problem:
> 
> I ran a community manipulation experiment with 7 reps of 2 treatments, for a 
> total of 14 communities. Communities 1-7 are in Treatment 1 and 8-14 are in 
> Treatment 2. I identified 5 taxa in the 14 communities and calculated a 
> community dissimilarity matrix (14*14). Now I would like to decompose the 
> distance matrix into a dataframe with the following column headings: 
> community1s, community2s, withinORbetweenTRT, and distance. “Within or 
> between treatment” indicates if the distance is between two communities 
> within the same treatment or between the two treatments (values of 0 or 1).
> 
> I did it by hand below to demonstrate, but my actual dataset has 100 
> communities so I need to figure out how to automate it...
> 
> df<-data.frame(cbind(1:14, 18:5, 3:16, 14:1, 16:3)) #random values
> 
> dist<-dist(df)
> 
> distance<-as.vector(dist)
> 
> community1s<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,
> 
> 4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,
> 
> 8,8,8,8,8,8,9,9,9,9,9,10,10,10,10,11,11,11,12,12,13)
> 
> community2s<-c(2,3,4,5,6,7,8,9,10,11,12,13,14,3,4,5,6,7,8,9,10,11,12,13,14,
> 
> 4,5,6,7,8,9,10,11,12,13,14,5,6,7,8,9,10,11,12,13,14,
> 
> 6,7,8,9,10,11,12,13,14,7,8,9,10,11,12,13,14,
> 
> 8,9,10,11,12,13,14,9,10,11,12,13,14,10,11,12,13,14,
> 
> 11,12,13,14,12,13,14,13,14,14)
> 
> #now I need a column for whether or not the comparison is within treatment or
> 
> #between treatments. I ordered the sites by treatment so sites 1-7 are in 
> treatment1
> 
> #and 8-14 are in treatment2. 0 is within and 1 is between.
> 
> withinORbetweenTRT<-c(0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
> 
> 1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,
> 
> 1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
> 
> 0,0)
> 
> #now I can assemble the dataframe:
> 
> final.df<-cbind(community1s, community2s, withinORbetweenTRT, distance)
> 
> final.df
> 
> I would appreciate any ideas!
> 
> -- 
> Kate Boersma, PhD
> Department of Biology
> University of San Diego
> 5998 Alcala Park
> San Diego CA 92110
> kateboer...@gmail.com
> http://www.oregonstate.edu/~boersmak/
> 
> Kate S. Boersma, Ph.D.
> kateboer...@gmail.com
> http://people.oregonstate.edu/~boersmak/
> 
> Department of Biology
> University of San Diego
> 5998 Alcala Park
> San Diego, CA 92110
> 
> 
>   [[alternative HTML version deleted]]
> 
> ___
> R-sig-ecology mailing list
> R-sig-ecology@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

___
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology


[R-sig-eco] Community distance matrix deconstruction

2014-12-12 Thread Kate Boersma
Hi all.

I have a community analysis data manipulation puzzle for you... hopefully 
someone can help. Please let me know if this question needs clarification, has 
previously been answered, or would be better sent to a different list.

Details follow.

Thank you,

Kate

---

Here is a simplified version of my problem:

I ran a community manipulation experiment with 7 reps of 2 treatments, for a 
total of 14 communities. Communities 1-7 are in Treatment 1 and 8-14 are in 
Treatment 2. I identified 5 taxa in the 14 communities and calculated a 
community dissimilarity matrix (14*14). Now I would like to decompose the 
distance matrix into a dataframe with the following column headings: 
community1s, community2s, withinORbetweenTRT, and distance. “Within or between 
treatment” indicates if the distance is between two communities within the same 
treatment or between the two treatments (values of 0 or 1).

I did it by hand below to demonstrate, but my actual dataset has 100 
communities so I need to figure out how to automate it...

df<-data.frame(cbind(1:14, 18:5, 3:16, 14:1, 16:3)) #random values

dist<-dist(df)

distance<-as.vector(dist)

community1s<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,

4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,

8,8,8,8,8,8,9,9,9,9,9,10,10,10,10,11,11,11,12,12,13)

community2s<-c(2,3,4,5,6,7,8,9,10,11,12,13,14,3,4,5,6,7,8,9,10,11,12,13,14,

4,5,6,7,8,9,10,11,12,13,14,5,6,7,8,9,10,11,12,13,14,

6,7,8,9,10,11,12,13,14,7,8,9,10,11,12,13,14,

8,9,10,11,12,13,14,9,10,11,12,13,14,10,11,12,13,14,

11,12,13,14,12,13,14,13,14,14)

#now I need a column for whether or not the comparison is within treatment or

#between treatments. I ordered the sites by treatment so sites 1-7 are in 
treatment1

#and 8-14 are in treatment2. 0 is within and 1 is between.

withinORbetweenTRT<-c(0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,

1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,

1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0)

#now I can assemble the dataframe:

final.df<-cbind(community1s, community2s, withinORbetweenTRT, distance)

final.df

I would appreciate any ideas!

-- 
Kate Boersma, PhD
Department of Biology
University of San Diego
5998 Alcala Park
San Diego CA 92110
kateboer...@gmail.com
http://www.oregonstate.edu/~boersmak/

Kate S. Boersma, Ph.D.
kateboer...@gmail.com
http://people.oregonstate.edu/~boersmak/

Department of Biology
University of San Diego
5998 Alcala Park
San Diego, CA 92110


[[alternative HTML version deleted]]

___
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology


Re: [R-sig-eco] How do I interpret linear mixed model contrast estimates from multcomp::glht()?

2014-12-12 Thread Matthew Van Scoyoc
​As I was suspecting, the answer is in contrast::contrast(). I'm going to
take this over to r-sig-mixed-models since that mailing list might be more
appropriate than ecology.​ Thanks Thierry, I appreciate the help.

Cheers

MVS
=
Matthew Van Scoyoc

https://sites.google.com/site/scoyoc/
=
Think SNOW!

On Fri, Dec 12, 2014 at 1:44 AM, ONKELINX, Thierry  wrote:

> Dear Matthew,
>
> The part before "== 0" are the rownames of the matrix passed to linfct.
> When the rownames are missing, the rownumbers are used.
>
> 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
> + 32 2 525 02 51
> + 32 54 43 61 85
> thierry.onkel...@inbo.be
> www.inbo.be
>
> 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
>
>
> -Oorspronkelijk bericht-
> Van: R-sig-ecology [mailto:r-sig-ecology-boun...@r-project.org] Namens
> Matthew Van Scoyoc
> Verzonden: vrijdag 12 december 2014 0:08
> Aan: r-sig-ecology@r-project.org
> Onderwerp: [R-sig-eco] How do I interpret linear mixed model contrast
> estimates from multcomp::glht()?
>
> So, what do the rows correspond to in the summary (e.g. "1 == 0")? I was
> thinking the answer was buried *cc*, but I can't figure it out. Consider
> this modified example I stole from here <
> https://stat.ethz.ch/pipermail/r-sig-mixed-models/2009q4/003061.html>...
>
> > options(contrasts = c(factor = "contr.SAS", ordered = "contr.poly"))
> > library("mlmRev")
> > library("lme4")
> > library("lmerTest")
> > library("contrast")
> > library("multcomp")
> >
> > data("egsingle")
> > # Linear mixed model
> > math.lmm <- lmer(math ~ year * size + female + (1|childid) +
> (1|schoolid), egsingle)
> > # Linear model
> > math.lm <- lm(math ~ year * size + female, data = egsingle) #
> > Calculate contrast matrix cc<-contrast(math.lm, a = list(year = c(.5,
> > 1.5, 2.5), size = 380, female
> = levels(egsingle$female)), +
> b = list(year = c(.5, 1.5,
> 2.5), size = 800, female = levels(egsingle$female)))
> > # Calculate estimates
> > summary(glht(math.lmm, linfct = cc$X))
>
>  Simultaneous Tests for General Linear Hypotheses
>
> Fit: lme4::lmer(formula = math ~ year * size + female + (1 | childid) +
> (1 | schoolid), data = egsingle)
>
> Linear Hypotheses:
>   Estimate   Std. Error   z value   Pr(>|z|)
> 1 == 0  0.127740.08020 1.593 0.1272
> 2 == 0  0.153220.08066 1.9000.0669 .
> 3 == 0  0.178700.08178 2.1850.0341 *
> 4 == 0  0.127740.08020 1.5930.1273
> 5 == 0  0.153220.08066 1.9000.0669 .
> 6 == 0  0.178700.08178 2.1850.0342 *
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Adjusted p
> values reported -- single-step method)
>
> Ultimately I would like to create a dataframe so I can plot the contrasts,
> something like this...
>
> > x = summary(glht(math.lmm, linfct = cc$X)) # Contrast data frame
> > math.contr = data.frame(Effect.Interaction = ..., Estimate =
> x[["test"]]$coefficients, Std.Error = x[["test"]]$sigma)
>
> Thanks for the help!
> Cheers,
> MVS
> =
> Matthew Van Scoyoc
>
> <
> https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=mvansco...@aggiemail.usu.edu
> >
> https://sites.google.com/site/scoyoc/
> =
> Think SNOW!
>
> [[alternative HTML version deleted]]
>
> ___
> R-sig-ecology mailing list
> R-sig-ecology@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
> Disclaimer Bezoek onze website / Visit our website<
> https://drupal.inbo.be/nl/disclaimer-mailberichten-van-het-inbo>
>

[[alternative HTML version deleted]]

___
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology


Re: [R-sig-eco] How do I interpret linear mixed model contrast estimates from multcomp::glht()?

2014-12-12 Thread ONKELINX, Thierry
Dear Matthew,

The part before "== 0" are the rownames of the matrix passed to linfct. When 
the rownames are missing, the rownumbers are used.

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
+ 32 2 525 02 51
+ 32 54 43 61 85
thierry.onkel...@inbo.be
www.inbo.be

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


-Oorspronkelijk bericht-
Van: R-sig-ecology [mailto:r-sig-ecology-boun...@r-project.org] Namens Matthew 
Van Scoyoc
Verzonden: vrijdag 12 december 2014 0:08
Aan: r-sig-ecology@r-project.org
Onderwerp: [R-sig-eco] How do I interpret linear mixed model contrast estimates 
from multcomp::glht()?

So, what do the rows correspond to in the summary (e.g. "1 == 0")? I was 
thinking the answer was buried *cc*, but I can't figure it out. Consider this 
modified example I stole from here 
...

> options(contrasts = c(factor = "contr.SAS", ordered = "contr.poly"))
> library("mlmRev")
> library("lme4")
> library("lmerTest")
> library("contrast")
> library("multcomp")
>
> data("egsingle")
> # Linear mixed model
> math.lmm <- lmer(math ~ year * size + female + (1|childid) +
(1|schoolid), egsingle)
> # Linear model
> math.lm <- lm(math ~ year * size + female, data = egsingle) #
> Calculate contrast matrix cc<-contrast(math.lm, a = list(year = c(.5,
> 1.5, 2.5), size = 380, female
= levels(egsingle$female)), +
b = list(year = c(.5, 1.5, 
2.5), size = 800, female = levels(egsingle$female)))
> # Calculate estimates
> summary(glht(math.lmm, linfct = cc$X))

 Simultaneous Tests for General Linear Hypotheses

Fit: lme4::lmer(formula = math ~ year * size + female + (1 | childid) +
(1 | schoolid), data = egsingle)

Linear Hypotheses:
  Estimate   Std. Error   z value   Pr(>|z|)
1 == 0  0.127740.08020 1.593 0.1272
2 == 0  0.153220.08066 1.9000.0669 .
3 == 0  0.178700.08178 2.1850.0341 *
4 == 0  0.127740.08020 1.5930.1273
5 == 0  0.153220.08066 1.9000.0669 .
6 == 0  0.178700.08178 2.1850.0342 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Adjusted p 
values reported -- single-step method)

Ultimately I would like to create a dataframe so I can plot the contrasts, 
something like this...

> x = summary(glht(math.lmm, linfct = cc$X)) # Contrast data frame
> math.contr = data.frame(Effect.Interaction = ..., Estimate =
x[["test"]]$coefficients, Std.Error = x[["test"]]$sigma)

Thanks for the help!
Cheers,
MVS
=
Matthew Van Scoyoc


https://sites.google.com/site/scoyoc/
=
Think SNOW!

[[alternative HTML version deleted]]

___
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Disclaimer Bezoek onze website / Visit our 
website
___
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology