mirror of
https://github.com/msberends/AMR.git
synced 2025-08-28 11:52:13 +02:00
new g.test() and edited freq()
This commit is contained in:
55
man/freq.Rd
55
man/freq.Rd
@@ -6,22 +6,24 @@
|
||||
\alias{top_freq}
|
||||
\title{Frequency table}
|
||||
\usage{
|
||||
freq(x, sort.count = TRUE, nmax = getOption("max.print.freq"),
|
||||
na.rm = TRUE, row.names = TRUE, markdown = FALSE,
|
||||
as.data.frame = FALSE, digits = 2, sep = " ")
|
||||
frequency_tbl(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"),
|
||||
na.rm = TRUE, row.names = TRUE, markdown = FALSE, digits = 2,
|
||||
sep = " ")
|
||||
|
||||
frequency_tbl(x, sort.count = TRUE, nmax = getOption("max.print.freq"),
|
||||
na.rm = TRUE, row.names = TRUE, markdown = FALSE,
|
||||
as.data.frame = FALSE, digits = 2, sep = " ")
|
||||
freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"),
|
||||
na.rm = TRUE, row.names = TRUE, markdown = FALSE, digits = 2,
|
||||
sep = " ")
|
||||
|
||||
top_freq(f, n)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{data}
|
||||
\item{x}{vector with items, or \code{data.frame}}
|
||||
|
||||
\item{sort.count}{sort on count. Use \code{FALSE} to sort alphabetically on item.}
|
||||
\item{...}{up to nine different columns of \code{x} to calculate frequencies from, see Examples}
|
||||
|
||||
\item{nmax}{number of row to print. The default, \code{15}, uses \code{\link{getOption}("max.print.freq")}. Use \code{nmax = 0} or \code{nmax = NA} to print all rows.}
|
||||
\item{sort.count}{sort on count, i.e. frequencies. Use \code{FALSE} to sort alphabetically on item.}
|
||||
|
||||
\item{nmax}{number of row to print. The default, \code{15}, uses \code{\link{getOption}("max.print.freq")}. Use \code{nmax = 0}, \code{nmax = NULL} or \code{nmax = NA} to print all rows.}
|
||||
|
||||
\item{na.rm}{a logical value indicating whether NA values should be removed from the frequency table. The header will always print the amount of \code{NA}s.}
|
||||
|
||||
@@ -29,24 +31,19 @@ top_freq(f, n)
|
||||
|
||||
\item{markdown}{print table in markdown format (this forces \code{nmax = NA})}
|
||||
|
||||
\item{as.data.frame}{return frequency table without header as a \code{data.frame} (e.g. to assign the table to an object)}
|
||||
|
||||
\item{digits}{how many significant digits are to be used for numeric values (not for the items themselves, that depends on \code{\link{getOption}("digits")})}
|
||||
\item{digits}{how many significant digits are to be used for numeric values in the header (not for the items themselves, that depends on \code{\link{getOption}("digits")})}
|
||||
|
||||
\item{sep}{a character string to separate the terms when selecting multiple columns}
|
||||
|
||||
\item{f}{a frequency table as \code{data.frame}, used as \code{freq(..., as.data.frame = TRUE)}}
|
||||
\item{f}{a frequency table}
|
||||
|
||||
\item{n}{number of top \emph{n} items to return, use -n for the bottom \emph{n} items. It will include more than \code{n} rows if there are ties.}
|
||||
}
|
||||
\value{
|
||||
\itemize{
|
||||
\item{When using \code{as.data.frame = FALSE} (default): only printed text}
|
||||
\item{When using \code{as.data.frame = TRUE}: a \code{data.frame} object with an additional class \code{"frequency_tbl"}}
|
||||
}
|
||||
A \code{data.frame} with an additional class \code{"frequency_tbl"}
|
||||
}
|
||||
\description{
|
||||
Create a frequency table of a vector of data, a single column or a maximum of 9 columns of a data frame. Supports markdown for reports. \code{top_freq} can be used to get the top/bottom \emph{n} items of a frequency table, with counts as names.
|
||||
Create a frequency table of a vector with items or a data frame. Supports quasiquotation and markdown for reports. \code{top_freq} can be used to get the top/bottom \emph{n} items of a frequency table, with counts as names.
|
||||
}
|
||||
\details{
|
||||
This package also has a vignette available about this function, run: \code{browseVignettes("AMR")} to read it.
|
||||
@@ -73,31 +70,41 @@ The function \code{top_freq} uses \code{\link[dplyr]{top_n}} internally and will
|
||||
\examples{
|
||||
library(dplyr)
|
||||
|
||||
# this all gives the same result:
|
||||
freq(septic_patients$hospital_id)
|
||||
freq(septic_patients[, "hospital_id"])
|
||||
septic_patients$hospital_id \%>\% freq()
|
||||
septic_patients[, "hospital_id"] \%>\% freq()
|
||||
septic_patients \%>\% freq("hospital_id")
|
||||
septic_patients \%>\% freq(hospital_id) # <- easiest to remember when used to tidyverse
|
||||
|
||||
# you could use `select`...
|
||||
septic_patients \%>\%
|
||||
filter(hospital_id == "A") \%>\%
|
||||
select(bactid) \%>\%
|
||||
freq()
|
||||
|
||||
# ... or you use `freq` to select it immediately
|
||||
septic_patients \%>\%
|
||||
filter(hospital_id == "A") \%>\%
|
||||
freq(bactid)
|
||||
|
||||
# select multiple columns; they will be pasted together
|
||||
septic_patients \%>\%
|
||||
left_join_microorganisms \%>\%
|
||||
filter(hospital_id == "A") \%>\%
|
||||
select(genus, species) \%>\%
|
||||
freq()
|
||||
freq(genus, species)
|
||||
|
||||
# save frequency table to an object
|
||||
years <- septic_patients \%>\%
|
||||
mutate(year = format(date, "\%Y")) \%>\%
|
||||
select(year) \%>\%
|
||||
freq(as.data.frame = TRUE)
|
||||
freq(year)
|
||||
years \%>\% pull(item)
|
||||
|
||||
# get top 10 bugs of hospital A as a vector
|
||||
septic_patients \%>\%
|
||||
filter(hospital_id == "A") \%>\%
|
||||
select(bactid) \%>\%
|
||||
freq(as.data.frame = TRUE) \%>\%
|
||||
freq(bactid) \%>\%
|
||||
top_freq(10)
|
||||
}
|
||||
\keyword{freq}
|
||||
|
108
man/g.test.Rd
Normal file
108
man/g.test.Rd
Normal file
@@ -0,0 +1,108 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/g.test.R
|
||||
\name{g.test}
|
||||
\alias{g.test}
|
||||
\title{\emph{G}-test of matrix or vector}
|
||||
\usage{
|
||||
g.test(x, y = NULL, alpha = 0.05, info = TRUE, minimum = 0)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{numeric vector or matrix}
|
||||
|
||||
\item{y}{expected value of \code{x}. Leave empty to determine automatically. This can also be ratios of \code{x}, e.g. calculated with \code{\link{vector2ratio}}.}
|
||||
|
||||
\item{alpha}{value to test the p value against}
|
||||
|
||||
\item{info}{logical to determine whether the analysis should be printed}
|
||||
|
||||
\item{minimum}{the test with fail if any of the observed values is below this value. Use \code{minimum = 30} for microbial epidemiology, to prevent calculating a p value when less than 30 isolates are available.}
|
||||
}
|
||||
\description{
|
||||
A \emph{G}-test can be used to see whether the number of observations in each category fits a theoretical expectation (called a \strong{\emph{G}-test of goodness-of-fit}), or to see whether the proportions of one variable are different for different values of the other variable (called a \strong{\emph{G}-test of independence}).
|
||||
}
|
||||
\section{\emph{G}-test of goodness-of-fit (likelihood ratio test)}{
|
||||
|
||||
Use the \emph{G}-test of goodness-of-fit when you have one nominal variable with two or more values (such as male and female, or red, pink and white flowers). You compare the observed counts of numbers of observations in each category with the expected counts, which you calculate using some kind of theoretical expectation (such as a 1:1 sex ratio or a 1:2:1 ratio in a genetic cross).
|
||||
|
||||
If the expected number of observations in any category is too small, the \emph{G}-test may give inaccurate results, and you should use an exact test instead. See the web page on small sample sizes for discussion of what "small" means.
|
||||
|
||||
The \emph{G}-test of goodness-of-fit is an alternative to the chi-square test of goodness-of-fit; each of these tests has some advantages and some disadvantages, and the results of the two tests are usually very similar.
|
||||
}
|
||||
|
||||
\section{\emph{G}-test of independence}{
|
||||
|
||||
Use the \emph{G}-test of independence when you have two nominal variables, each with two or more possible values. You want to know whether the proportions for one variable are different among values of the other variable.
|
||||
|
||||
It is also possible to do a \emph{G}-test of independence with more than two nominal variables. For example, Jackson et al. (2013) also had data for children under 3, so you could do an analysis of old vs. young, thigh vs. arm, and reaction vs. no reaction, all analyzed together.
|
||||
|
||||
Fisher's exact test is more accurate than the \emph{G}-test of independence when the expected numbers are small, so it is recommend to only use the \emph{G}-test if your total sample size is greater than 1000.
|
||||
|
||||
The \emph{G}-test of independence is an alternative to the chi-square test of independence, and they will give approximately the same results.
|
||||
}
|
||||
|
||||
\section{How the test works}{
|
||||
|
||||
Unlike the exact test of goodness-of-fit, the \emph{G}-test does not directly calculate the probability of obtaining the observed results or something more extreme. Instead, like almost all statistical tests, the \emph{G}-test has an intermediate step; it uses the data to calculate a test statistic that measures how far the observed data are from the null expectation. You then use a mathematical relationship, in this case the chi-square distribution, to estimate the probability of obtaining that value of the test statistic.
|
||||
|
||||
The \emph{G}-test uses the log of the ratio of two likelihoods as the test statistic, which is why it is also called a likelihood ratio test or log-likelihood ratio test. The formula to calculate a \emph{G}-statistic is:
|
||||
|
||||
\code{G <- 2 * sum(x * log(x / x.expected))}
|
||||
|
||||
Since this is chi-square distributed, the p value can be calculated with:
|
||||
|
||||
\code{p <- 1 - stats::pchisq(G, df))}
|
||||
|
||||
where \code{df} are the degrees of freedom: \code{max(NROW(x) - 1, 1) * max(NCOL(x) - 1, 1)}.
|
||||
|
||||
If there are more than two categories and you want to find out which ones are significantly different from their null expectation, you can use the same method of testing each category vs. the sum of all categories, with the Bonferroni correction. You use \emph{G}-tests for each category, of course.
|
||||
}
|
||||
|
||||
\examples{
|
||||
# = EXAMPLE 1 =
|
||||
# Shivrain et al. (2006) crossed clearfield rice (which are resistant
|
||||
# to the herbicide imazethapyr) with red rice (which are susceptible to
|
||||
# imazethapyr). They then crossed the hybrid offspring and examined the
|
||||
# F2 generation, where they found 772 resistant plants, 1611 moderately
|
||||
# resistant plants, and 737 susceptible plants. If resistance is controlled
|
||||
# by a single gene with two co-dominant alleles, you would expect a 1:2:1
|
||||
# ratio.
|
||||
|
||||
x <- c(772, 1611, 737)
|
||||
x.expected <- vector2ratio(x, ratio = "1:2:1")
|
||||
x.expected
|
||||
# 780 1560 780
|
||||
|
||||
g.test(x, x.expected)
|
||||
# p = 0.12574.
|
||||
|
||||
# There is no significant difference from a 1:2:1 ratio.
|
||||
# Meaning: resistance controlled by a single gene with two co-dominant
|
||||
# alleles, is plausible.
|
||||
|
||||
|
||||
# = EXAMPLE 2 =
|
||||
# Red crossbills (Loxia curvirostra) have the tip of the upper bill either
|
||||
# right or left of the lower bill, which helps them extract seeds from pine
|
||||
# cones. Some have hypothesized that frequency-dependent selection would
|
||||
# keep the number of right and left-billed birds at a 1:1 ratio. Groth (1992)
|
||||
# observed 1752 right-billed and 1895 left-billed crossbills.
|
||||
|
||||
x <- c(1752, 1895)
|
||||
x.expected <- vector2ratio(x, ratio = c(1, 1))
|
||||
x.expected
|
||||
# 1823.5 1823.5
|
||||
|
||||
g.test(x, x.expected)
|
||||
# p = 0.01787343
|
||||
|
||||
# There is a significant difference from a 1:1 ratio.
|
||||
# Meaning: there are significantly more left-billed birds.
|
||||
|
||||
}
|
||||
\references{
|
||||
McDonald, J.H. 2014. \strong{Handbook of Biological Statistics (3rd ed.)}. Sparky House Publishing, Baltimore, Maryland. \url{http://www.biostathandbook.com/gtestgof.html}.
|
||||
}
|
||||
\seealso{
|
||||
\code{\link{chisq.test}}
|
||||
}
|
||||
\keyword{chi}
|
19
man/p.symbol.Rd
Normal file
19
man/p.symbol.Rd
Normal file
@@ -0,0 +1,19 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/p.symbol.R
|
||||
\name{p.symbol}
|
||||
\alias{p.symbol}
|
||||
\title{Symbol of a p value}
|
||||
\usage{
|
||||
p.symbol(p, emptychar = " ")
|
||||
}
|
||||
\arguments{
|
||||
\item{p}{p value}
|
||||
|
||||
\item{emptychar}{text to show when \code{p > 0.1}}
|
||||
}
|
||||
\value{
|
||||
Text
|
||||
}
|
||||
\description{
|
||||
Return the symbol related to the p value: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
|
||||
}
|
14
man/print.Rd
14
man/print.Rd
@@ -1,25 +1,27 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/print.R
|
||||
\name{print}
|
||||
% Please edit documentation in R/freq.R, R/print.R
|
||||
\name{print.frequency_tbl}
|
||||
\alias{print.frequency_tbl}
|
||||
\alias{print}
|
||||
\alias{print.tbl_df}
|
||||
\alias{print.tbl}
|
||||
\alias{print.frequency_tbl}
|
||||
\alias{print.data.table}
|
||||
\title{Printing Data Tables and Tibbles}
|
||||
\usage{
|
||||
\method{print}{frequency_tbl}(x, ...)
|
||||
|
||||
\method{print}{tbl_df}(x, nmax = 10, header = TRUE, row.names = TRUE,
|
||||
right = FALSE, width = 1, na = "<NA>", ...)
|
||||
|
||||
\method{print}{tbl}(x, ...)
|
||||
|
||||
\method{print}{frequency_tbl}(x, ...)
|
||||
|
||||
\method{print}{data.table}(x, print.keys = FALSE, ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{object of class \code{data.frame}.}
|
||||
|
||||
\item{...}{optional arguments to \code{print} or \code{plot} methods.}
|
||||
|
||||
\item{nmax}{amount of rows to print in total. When the total amount of rows exceeds this limit, the first and last \code{nmax / 2} rows will be printed. Use \code{nmax = NA} to print all rows.}
|
||||
|
||||
\item{header}{print header with information about data size and tibble grouping}
|
||||
@@ -34,8 +36,6 @@
|
||||
|
||||
\item{na}{value to print instead of NA}
|
||||
|
||||
\item{...}{optional arguments to \code{print} or \code{plot} methods.}
|
||||
|
||||
\item{print.keys}{print keys for \code{data.table}}
|
||||
}
|
||||
\description{
|
||||
|
64
man/vector2ratio.Rd
Normal file
64
man/vector2ratio.Rd
Normal file
@@ -0,0 +1,64 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/g.test.R
|
||||
\name{vector2ratio}
|
||||
\alias{vector2ratio}
|
||||
\title{Transform vector to ratio}
|
||||
\usage{
|
||||
vector2ratio(x, ratio)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{vector of values}
|
||||
|
||||
\item{ratio}{vector with ratios of \code{x} and with same length (like \code{ratio = c(1, 2, 1)}) or a text with characters \code{":"}, \code{"-"} or \code{","} (like \code{ratio = "1:2:1"} or even \code{ratio = "1:2:1.25"})}
|
||||
}
|
||||
\description{
|
||||
Transform vector to ratio
|
||||
}
|
||||
\examples{
|
||||
# = EXAMPLE 1 =
|
||||
# Shivrain et al. (2006) crossed clearfield rice (which are resistant
|
||||
# to the herbicide imazethapyr) with red rice (which are susceptible to
|
||||
# imazethapyr). They then crossed the hybrid offspring and examined the
|
||||
# F2 generation, where they found 772 resistant plants, 1611 moderately
|
||||
# resistant plants, and 737 susceptible plants. If resistance is controlled
|
||||
# by a single gene with two co-dominant alleles, you would expect a 1:2:1
|
||||
# ratio.
|
||||
|
||||
x <- c(772, 1611, 737)
|
||||
x.expected <- vector2ratio(x, ratio = "1:2:1")
|
||||
x.expected
|
||||
# 780 1560 780
|
||||
|
||||
g.test(x, x.expected)
|
||||
# p = 0.12574.
|
||||
|
||||
# There is no significant difference from a 1:2:1 ratio.
|
||||
# Meaning: resistance controlled by a single gene with two co-dominant
|
||||
# alleles, is plausible.
|
||||
|
||||
|
||||
# = EXAMPLE 2 =
|
||||
# Red crossbills (Loxia curvirostra) have the tip of the upper bill either
|
||||
# right or left of the lower bill, which helps them extract seeds from pine
|
||||
# cones. Some have hypothesized that frequency-dependent selection would
|
||||
# keep the number of right and left-billed birds at a 1:1 ratio. Groth (1992)
|
||||
# observed 1752 right-billed and 1895 left-billed crossbills.
|
||||
|
||||
x <- c(1752, 1895)
|
||||
x.expected <- vector2ratio(x, ratio = c(1, 1))
|
||||
x.expected
|
||||
# 1823.5 1823.5
|
||||
|
||||
g.test(x, x.expected)
|
||||
# p = 0.01787343
|
||||
|
||||
# There is a significant difference from a 1:1 ratio.
|
||||
# Meaning: there are significantly more left-billed birds.
|
||||
|
||||
}
|
||||
\references{
|
||||
McDonald, J.H. 2014. \strong{Handbook of Biological Statistics (3rd ed.)}. Sparky House Publishing, Baltimore, Maryland.
|
||||
}
|
||||
\seealso{
|
||||
\code{\link{g.test}}
|
||||
}
|
Reference in New Issue
Block a user