[R] Order output list od TukeyHSD function by "p adj"

Sergio Fonda sergio.fonda99 at gmail.com
Wed Feb 24 18:09:43 CET 2016


Thanks for your very useful help!
It's working on my data (apply() to a list of lists ....)

All the best
Sergio


2016-02-24 17:02 GMT+01:00 David L Carlson <dcarlson at tamu.edu>:

> hsd.fit is list containing 1 element, a matrix called "wool:tension". It
> will be simpler if you extract the matrix and then use order() to get the
> matrix sorted by p adj:
>
> > table <- hsd.fit[["wool:tension"]]
> > table[order(table[, 'p adj']), ]
>               diff        lwr      upr        p adj
> A:L-B:H 25.7777778  10.471456 41.08410 0.0001136469
> A:L-A:M 20.5555556   5.249234 35.86188 0.0029580438
> A:L-A:H 20.0000000   4.693678 35.30632 0.0040954674
> A:L-B:L 16.3333333   1.027012 31.63966 0.0302143219
> A:L-B:M 15.7777778   0.471456 31.08410 0.0398172376
> B:M-B:H 10.0000000  -5.306322 25.30632 0.3918766902
> B:L-B:H  9.4444444  -5.861877 24.75077 0.4560949981
> A:H-B:H  5.7777778  -9.528544 21.08410 0.8705571533
> A:M-B:H  5.2222222 -10.084100 20.52854 0.9114780002
> B:M-A:M  4.7777778 -10.528544 20.08410 0.9377205494
> B:L-A:M  4.2222222 -11.084100 19.52854 0.9626540845
> B:M-A:H  4.2222222 -11.084100 19.52854 0.9626540845
> B:L-A:H  3.6666667 -11.639655 18.97299 0.9797122861
> A:H-A:M  0.5555556 -14.750766 15.86188 0.9999978240
> B:M-B:L  0.5555556 -14.750766 15.86188 0.9999978240
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sergio
> Fonda
> Sent: Wednesday, February 24, 2016 6:47 AM
> To: Jim Lemon
> Cc: r-help at r-project.org
> Subject: Re: [R] Order output list od TukeyHSD function by "p adj"
>
> Thank you Jim also for introducing a shorter data frame.
> However the HSD output I deal with is derived from a crossing factors
> condition.
> Could you kindly explain how could I sort results obtained from a
>
> fm1 <- aov(breaks ~ wool * tension, data = warpbreaks)
> hsd.fit<-TukeyHSD(fm1, "wool:tension", ordered = TRUE)
>
> which gives the following "crossing" output where "wool:tension" is a
> string
>
> $`wool:tension`
>               diff        lwr      upr     p adj
> A:M-B:H  5.2222222 -10.084100 20.52854 0.9114780
> A:H-B:H  5.7777778  -9.528544 21.08410 0.8705572
> B:L-B:H  9.4444444  -5.861877 24.75077 0.4560950
> B:M-B:H 10.0000000  -5.306322 25.30632 0.3918767
> A:L-B:H 25.7777778  10.471456 41.08410 0.0001136
> A:H-A:M  0.5555556 -14.750766 15.86188 0.9999978
> B:L-A:M  4.2222222 -11.084100 19.52854 0.9626541
> B:M-A:M  4.7777778 -10.528544 20.08410 0.9377205
> A:L-A:M 20.5555556   5.249234 35.86188 0.0029580
> B:L-A:H  3.6666667 -11.639655 18.97299 0.9797123
> B:M-A:H  4.2222222 -11.084100 19.52854 0.9626541
> A:L-A:H 20.0000000   4.693678 35.30632 0.0040955
> B:M-B:L  0.5555556 -14.750766 15.86188 0.9999978
> A:L-B:L 16.3333333   1.027012 31.63966 0.0302143
> A:L-B:M 15.7777778   0.471456 31.08410 0.0398172
>
>
> How may I order this part of results by "p adj" ?
> Thank you again for your patience!
> Sergio
>
> 2016-02-24 7:48 GMT+01:00 Jim Lemon <drjimlemon at gmail.com>:
>
> > Hi Sergio,
> > I couldn't get your example data to read in, so I have used the example
> in
> > the help page:
> >
> > fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)
> > hsd.fit<-TukeyHSD(fm1, "tension", ordered = TRUE)
> > hsd.fit$tension[order(hsd.fit$tension[,4]),]
> >         diff        lwr      upr       p adj
> > L-H 14.722222  5.3688015 24.07564 0.001121788
> > L-M 10.000000  0.6465793 19.35342 0.033626219
> > M-H  4.722222 -4.6311985 14.07564 0.447421021
> >
> > Obviously you would have to examine the output of TukeyHSD with str() to
> > sort out which column to use for ordering.
> >
> > Jim
> >
> >
> > On Wed, Feb 24, 2016 at 11:21 AM, Sergio Fonda <sergio.fonda99 at gmail.com
> >
> > wrote:
> >
> >>  Hello, It's already for several hours that I try to order the list
> >> obtained by the function TukeyHSD according to the variable "p adj"
> >> (in ascending order). Unfortunately, without success.
> >> In addition to following two lines of code, that offer the result but
> >> separately so do not correspond to the desired result, I was unable to
> >> go:
> >> DF.5 <-lapply(DF.4, function (x)
> as.data.frame(x[c("patient:Fold.fac")]))
> >> DF.6 <- DF.5[[1]][order(DF.5[[1]]$patient.Fold.fac.p.adj),]
> >> Please, I ask some help to answer these two questions:
> >> 1) is it possible to get directly from the function TukeyHSD sorted
> >> rows by "p adj"?
> >> 2) or, may the output list from TukeyHSD() be processed (e.g. by
> >> lapply) to sort its elements according to "p adj"?
> >> I attach at bottom a simulation of a list obtained from TukeyHSD which
> >> should be ordered by "p adj".
> >> Thanks in advance for any suggestion!
> >> Sergio
> >> #
> >> > dput(DF.4)
> >> list(structure(list(patient = structure(c(12289274.0619908,
> >> -2380308.48287107,
> >> -14669582.5448618, -4176414.56676197, -18845997.1116238,
> >> -31135271.1736146,
> >> 28754962.6907435, 14085380.1458817, 1796106.0838909, 0.186808233632622,
> >> 0.938592742253258, 0.0922160074633905), .Dim = 3:4, .Dimnames = list(
> >>     c("PARTIAL-COMPLETE", "NO-COMPLETE", "NO-PARTIAL"), c("diff",
> >>     "lwr", "upr", "p adj"))), Fold.fac = structure(c(-12697325.7957036,
> >> -23938561.4288898, -1456090.1625174, 0.0268617694934425), .Dim = c(1L,
> >> 4L), .Dimnames = list("middle-low", c("diff", "lwr", "upr", "p adj"
> >> ))), `patient:Fold.fac` = structure(c(15369710.0977205,
> 6521960.91205235,
> >> -4695802.45257667, 4502968.78925385, -16007472.7140147,
> -8847749.18566819,
> >> -20065512.5502972, -10866741.3084667, -31377182.8117352,
> -11217763.364629,
> >> -2018992.12279849, -22529433.626067, 9198771.24183052, -11311670.261438,
> >> -20510441.5032685, -12927630.9811041, -21775380.1667723,
> >> -33016252.8378897,
> >> -23817481.5960591, -44327923.0993277, -37145090.2644928,
> >> -48385962.9356102,
> >> -39187191.6937797, -59697633.1970482, -39538213.749942,
> -30339442.5081115,
> >> -50849884.01138, -19144769.6082929, -39655211.1115614, -48853982.353392,
> >> 43667051.1765452, 34819301.990877, 23624647.9327363, 32823419.1745669,
> >> 12312977.6712983, 19449591.8931564, 8254937.83501579, 17453709.0768463,
> >> -3056732.42642223, 17102687.020684, 26301458.2625145, 5791016.75924596,
> >> 37542312.0919539, 17031870.5886854, 7833099.34685487, 0.632098034657304,
> >> 0.986399530577416, 0.997064140940244, 0.997595806079891,
> >> 0.590366152539712,
> >> 0.948510666498818, 0.330512619876425, 0.883673837089478,
> >> 0.0198821692150445,
> >> 0.868982868764254, 0.999952294188371, 0.207190890959777,
> >> 0.939934594322161,
> >> 0.86529009598038, 0.306625751897378), .Dim = c(15L, 4L), .Dimnames =
> list(
> >>     c("PARTIAL:low-COMPLETE:low", "NO:low-COMPLETE:low",
> >> "COMPLETE:middle-COMPLETE:low",
> >>     "PARTIAL:middle-COMPLETE:low", "NO:middle-COMPLETE:low",
> >>     "NO:low-PARTIAL:low", "COMPLETE:middle-PARTIAL:low",
> >> "PARTIAL:middle-PARTIAL:low",
> >>     "NO:middle-PARTIAL:low", "COMPLETE:middle-NO:low",
> >> "PARTIAL:middle-NO:low",
> >>     "NO:middle-NO:low", "PARTIAL:middle-COMPLETE:middle",
> >> "NO:middle-COMPLETE:middle",
> >>     "NO:middle-PARTIAL:middle"), c("diff", "lwr", "upr", "p adj"
> >>     )))), .Names = c("patient", "Fold.fac", "patient:Fold.fac"
> >> ), class = c("TukeyHSD", "multicomp"), orig.call = aov(formula =
> >> abundance ~
> >>     patient * Fold.fac, data = x), conf.level = 0.95, ordered = FALSE),
> >>     structure(list(patient = structure(c(11084928.3849924,
> >> -3790273.898858,
> >>     -14875202.2838504, -2565656.8579769, -17440859.1418273,
> >> -28525787.5268197,
> >>     24735513.6279617, 9860311.34411127, -1224617.0408811,
> >> 0.137587687259541,
> >>     0.791659281224941, 0.028733410253219), .Dim = 3:4, .Dimnames = list(
> >>         c("PARTIAL-COMPLETE", "NO-COMPLETE", "NO-PARTIAL"), c("diff",
> >>         "lwr", "upr", "p adj"))), Fold.fac =
> structure(c(25003217.9525667,
> >>     15683872.2084017, 34322563.6967316, 1.59282125378191e-07), .Dim =
> >> c(1L,
> >>     4L), .Dimnames = list("low-high", c("diff", "lwr", "upr",
> >>     "p adj"))), `patient:Fold.fac` = structure(c(6786144.11764773,
> >>     -14136208.8235292, 15255972.7140153, 30625682.8117357,
> >> 21777933.6260674,
> >>     -20922352.9411769, 8469828.59636761, 23839538.6940879,
> >> 14991789.5084197,
> >>     29392181.5375445, 44761891.6352649, 35914142.4495966,
> >> 15369710.0977203,
> >>     6521960.91205206, -8847749.18566828, -16711562.4882314,
> >> -37633915.4294083,
> >>     -8222591.1541805, 7147118.94353984, -1700630.24212845,
> >> -44420059.547056,
> >>     -15008735.2718282, 360974.825892106, -8486774.35977618,
> >> 5913617.6693487,
> >>     21283327.767069, 12435578.5814008, -8089695.41243546,
> >> -16937444.5981037,
> >>     -32307154.6958241, 30283850.7235268, 9361497.78234989,
> >> 38734536.5822112,
> >>     54104246.6799315, 45256497.4942632, 2575353.66470216,
> >> 31948392.4645634,
> >>     47318102.5622838, 38470353.3766155, 52870745.4057404,
> >> 68240455.5034607,
> >>     59392706.3177924, 38829115.6078761, 29981366.4222079,
> >> 14611656.3244875,
> >>     0.963180623746077, 0.521122619371869, 0.431445001590424,
> >>     0.00280179326576413, 0.0869916657428951, 0.113173926329674,
> >>     0.908231706478258, 0.0441523667992262, 0.451984651996765,
> >>     0.00489618219443777, 9.080596613531e-07, 0.000195613055067767,
> >>     0.42174023314457, 0.968747601969596, 0.891038899833401), .Dim =
> c(15L,
> >>     4L), .Dimnames = list(c("PARTIAL:high-COMPLETE:high",
> >> "NO:high-COMPLETE:high",
> >>     "COMPLETE:low-COMPLETE:high", "PARTIAL:low-COMPLETE:high",
> >>     "NO:low-COMPLETE:high", "NO:high-PARTIAL:high",
> >> "COMPLETE:low-PARTIAL:high",
> >>     "PARTIAL:low-PARTIAL:high", "NO:low-PARTIAL:high",
> >> "COMPLETE:low-NO:high",
> >>     "PARTIAL:low-NO:high", "NO:low-NO:high", "PARTIAL:low-COMPLETE:low",
> >>     "NO:low-COMPLETE:low", "NO:low-PARTIAL:low"), c("diff", "lwr",
> >>     "upr", "p adj")))), .Names = c("patient", "Fold.fac",
> >> "patient:Fold.fac"
> >>     ), class = c("TukeyHSD", "multicomp"), orig.call = aov(formula =
> >> abundance ~
> >>         patient * Fold.fac, data = x), conf.level = 0.95, ordered =
> >> FALSE))
> >> >
> >>
> >> ______________________________________________
> >> R-help at 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 at 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]]



More information about the R-help mailing list