Sorkin, John
2020-Jun-12 21:34 UTC
[R] Obtaining p values from t-test run with a by function
Colleagues,
I am trying to retrieve the p values produced by a Student's t-test run
using a by function, but can not do so. I can easily get the p value when I run
s Student's t-test without a by function. What is the secret to obtaining
results returned from a function run within a by function.
An annotated repeatable example (including data) can be found below.
Thank you,
John
# Test data
mydata <- structure(list(Group = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 2L), .Label = c("EPA", "P"),
class = "factor"),
WtBaseline = c(76.6, 73.8, 77.6, 91.7, 110.3, 121.7, 82.1,
82.8, 119, 88.4, 75.7, 71.4, 72.1)), class = "data.frame",
row.names = c(NA,-13L))
cat("This is what mydata looks like\n")
mydata
result <- by(mydata$WtBaseline,mydata$Group,t.test)
cat("Student's t-test run using by command\n")
cat("Result has results for both groups, EPA and P\n")
result
cat("I can isolate the collective results for group EPA\n")
result[1]
cat("I can isolate the collective results for group P\n")
result[2]
cat("I cant get the p-values for the gruops")
result[1]$p.value
result[2]$p.value
cat("When run without by function, one can get the p value\n")
xxx <- t.test(WtBaseline~Group,data=mydata)
cat("t-test run without by fundtion\n")
xxx
cat("p value isolated from t-test run without by function\n")
xxx$p.value
John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric
Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
[[alternative HTML version deleted]]
Stephen Ellison
2020-Jun-12 22:10 UTC
[R] Obtaining p values from t-test run with a by function
Define a wrapper function for the t test that only returns the p-value?
by(mydata$WtBaseline,mydata$Group, function(...) t.test(...)$p.value)
S Ellison
________________________________________
From: R-help [r-help-bounces at r-project.org] on behalf of Sorkin, John
[jsorkin at som.umaryland.edu]
Sent: 12 June 2020 22:34
To: r-help at r-project.org (r-help at r-project.org)
Subject: [R] Obtaining p values from t-test run with a by function
============== EXTERNAL EMAIL
==============
Colleagues,
I am trying to retrieve the p values produced by a Student's t-test run
using a by function, but can not do so. I can easily get the p value when I run
s Student's t-test without a by function. What is the secret to obtaining
results returned from a function run within a by function.
An annotated repeatable example (including data) can be found below.
Thank you,
John
# Test data
mydata <- structure(list(Group = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 2L), .Label = c("EPA", "P"),
class = "factor"),
WtBaseline = c(76.6, 73.8, 77.6, 91.7, 110.3, 121.7, 82.1,
82.8, 119, 88.4, 75.7, 71.4, 72.1)), class = "data.frame",
row.names = c(NA,-13L))
cat("This is what mydata looks like\n")
mydata
result <- by(mydata$WtBaseline,mydata$Group,t.test)
cat("Student's t-test run using by command\n")
cat("Result has results for both groups, EPA and P\n")
result
cat("I can isolate the collective results for group EPA\n")
result[1]
cat("I can isolate the collective results for group P\n")
result[2]
cat("I cant get the p-values for the gruops")
result[1]$p.value
result[2]$p.value
cat("When run without by function, one can get the p value\n")
xxx <- t.test(WtBaseline~Group,data=mydata)
cat("t-test run without by fundtion\n")
xxx
cat("p value isolated from t-test run without by function\n")
xxx$p.value
John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric
Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
[[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.
=============================================================================================WARNING
- EXTERNAL: This email originated from outside of LGC. Do not click any links or
open any attachments
unless you trust the sender and know that the content is safe
=============================================================================================
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
Sarah Goslee
2020-Jun-12 22:20 UTC
[R] Obtaining p values from t-test run with a by function
Where you have result[1]$p.value result[2]$p.value You need result[[1]]$p.value result[[2]]$p.value to get the first component of the list. Sarah On Fri, Jun 12, 2020 at 5:35 PM Sorkin, John <jsorkin at som.umaryland.edu> wrote:> > Colleagues, > I am trying to retrieve the p values produced by a Student's t-test run using a by function, but can not do so. I can easily get the p value when I run s Student's t-test without a by function. What is the secret to obtaining results returned from a function run within a by function. > > An annotated repeatable example (including data) can be found below. > Thank you, > John > > > # Test data > mydata <- structure(list(Group = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 1L, > 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("EPA", "P"), class = "factor"), > WtBaseline = c(76.6, 73.8, 77.6, 91.7, 110.3, 121.7, 82.1, > 82.8, 119, 88.4, 75.7, 71.4, 72.1)), class = "data.frame", row.names = c(NA,-13L)) > > cat("This is what mydata looks like\n") > mydata > > result <- by(mydata$WtBaseline,mydata$Group,t.test) > cat("Student's t-test run using by command\n") > cat("Result has results for both groups, EPA and P\n") > result > > cat("I can isolate the collective results for group EPA\n") > result[1] > cat("I can isolate the collective results for group P\n") > result[2] > > cat("I cant get the p-values for the gruops") > result[1]$p.value > result[2]$p.value > > cat("When run without by function, one can get the p value\n") > xxx <- t.test(WtBaseline~Group,data=mydata) > cat("t-test run without by fundtion\n") > xxx > cat("p value isolated from t-test run without by function\n") > xxx$p.value > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > [[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.-- Sarah Goslee (she/her) http://www.numberwright.com
Rui Barradas
2020-Jun-13 05:20 UTC
[R] Obtaining p values from t-test run with a by function
Hello, Or in one go with *apply, function '[[': sapply(result, '[[', 'p.value') # EPA P #2.564503e-04 4.173480e-06 Hope this helps, Rui Barradas ?s 23:20 de 12/06/20, Sarah Goslee escreveu:> Where you have > > result[1]$p.value > result[2]$p.value > > You need > > result[[1]]$p.value > result[[2]]$p.value > > to get the first component of the list. > > Sarah > > On Fri, Jun 12, 2020 at 5:35 PM Sorkin, John <jsorkin at som.umaryland.edu> wrote: >> >> Colleagues, >> I am trying to retrieve the p values produced by a Student's t-test run using a by function, but can not do so. I can easily get the p value when I run s Student's t-test without a by function. What is the secret to obtaining results returned from a function run within a by function. >> >> An annotated repeatable example (including data) can be found below. >> Thank you, >> John >> >> >> # Test data >> mydata <- structure(list(Group = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 1L, >> 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("EPA", "P"), class = "factor"), >> WtBaseline = c(76.6, 73.8, 77.6, 91.7, 110.3, 121.7, 82.1, >> 82.8, 119, 88.4, 75.7, 71.4, 72.1)), class = "data.frame", row.names = c(NA,-13L)) >> >> cat("This is what mydata looks like\n") >> mydata >> >> result <- by(mydata$WtBaseline,mydata$Group,t.test) >> cat("Student's t-test run using by command\n") >> cat("Result has results for both groups, EPA and P\n") >> result >> >> cat("I can isolate the collective results for group EPA\n") >> result[1] >> cat("I can isolate the collective results for group P\n") >> result[2] >> >> cat("I cant get the p-values for the gruops") >> result[1]$p.value >> result[2]$p.value >> >> cat("When run without by function, one can get the p value\n") >> xxx <- t.test(WtBaseline~Group,data=mydata) >> cat("t-test run without by fundtion\n") >> xxx >> cat("p value isolated from t-test run without by function\n") >> xxx$p.value >> >> >> John David Sorkin M.D., Ph.D. >> Professor of Medicine >> Chief, Biostatistics and Informatics >> University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine >> Baltimore VA Medical Center >> 10 North Greene Street >> GRECC (BT/18/GR) >> Baltimore, MD 21201-1524 >> (Phone) 410-605-7119 >> (Fax) 410-605-7913 (Please call phone number above prior to faxing) >> >> >> [[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. > > >