Skip to content

Commit

Permalink
Some small changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmueller18 committed Aug 27, 2023
1 parent 2eb2036 commit 4f20272
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ data-raw
^doc$
^Meta$
^\.github$
^CRAN-SUBMISSION$
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: mortAAR
Type: Package
Title: Analysis of Archaeological Mortality Data
Version: 1.1.4
Version: 1.1.5
Authors@R: c(
person("Nils", "Mueller-Scheessel", email = "nils.mueller-scheessel@ufg.uni-kiel.de", role = c("aut", "cre", "cph")),
person("Martin", "Hinz", email = "martin.hinz@ufg.uni-kiel.de", role = c("aut")),
Expand All @@ -19,7 +19,7 @@ Description: A collection of functions for the analysis of archaeological mortal
<https://books.google.de/books?id=nG5FoO_becAC&lpg=PA27&ots=LG0b_xrx6O&dq=life%20table%20archaeology&pg=PA27#v=onepage&q&f=false>).
It takes demographic data in different formats and displays the result in a standard life table
as well as plots the relevant indices (percentage of deaths, survivorship, probability of death, life expectancy, percentage of population).
Date: 2023-07-17
Date: 2023-08-27
License: GPL-3 | file LICENSE
Encoding: UTF-8
LazyData: true
Expand All @@ -28,7 +28,8 @@ Imports:
Rdpack (>= 0.4-20),
reshape2 (>= 1.4.2),
methods (>= 3.3.3),
tibble (>= 3.0.3)
tibble (>= 3.0.3),
rlang (>= 1.1.1)
RoxygenNote: 7.2.3
Suggests:
testthat,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# mortAAR 1.1.5
- fixed an error in plotting that occurred after the fix of "aes_string" of ggplot2
- enabled easier input of known-age data-sets

# mortAAR 1.1.4
- fixed an invalid url

Expand Down
44 changes: 30 additions & 14 deletions R/input_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ NA_to_0 <- function(x) {
#' If no life table exists, this function will process a dataframe including the age ranges of
#' individuals or groups of individuals to discrete the age classes. The age range is spread to
#' single years. \bold{agebeg} has to be specified for the beginning of an age range, as well
#' as \bold{ageend} for the end of an age range. The \bold{method} defines in which way the single years between the different age classes are split.
#' If the data set comprises a grouping variable (e.g., sex), this can be specified with \bold{group}.
#' as \bold{ageend} for the end of an age range. If a data-set with year-known individuals is
#' used, \bold{ageend} can be omitted but then the parameter \bold{agerange} has to left on its
#' default value (\code{included}). The \bold{method} defines in which way the single years between
#' the different age classes are split. If the data set comprises a grouping variable (e.g., sex),
#' this can be specified with \bold{group}.
#'
#' @param x single dataframe containing sex age and quantity of deceased (individuals or group of individuals).
#' @param dec column name (as character) of the count of deceased, optional.
#' @param agebeg column name (as character) for the beginning of an age range.
#' @param ageend column name (as character) for the end of an age range.
#' @param ageend column name (as character) for the end of an age range, optional.
#' @param group column name (as character) of the grouping field (e.g., sex),
#' optional. Default setup is: \code{NA}.
#' @param method character string, optional. Default options is \code{Standard}, which will create age classes beginning with 1 year,
#' up to 4 years, followed by steps of 5 years (1,4,5,5,...) until the maximum age is reached. \code{Equal5} will create age classes with an even distribution, stepped by 5 years (5,5,...) until the maximum age is reached.
#' @param method character string, optional. Default options is \code{Standard}, which will create age
#' classes beginning with 1 year, up to 4 years, followed by steps of 5 years (1,4,5,5,...) until the
#' maximum age is reached. \code{Equal5} will create age classes with an even distribution, stepped
#' by 5 years (5,5,...) until the maximum age is reached. If method is a single numeric, this number will be
#' repeated until the maximum age is reached. Thereby, it is possible to create a year-wise life table.
#' @param agerange character string, optional. Default setup is: \code{included}.
#' If the age ranges from "20 to 40" and "40 to 60", \code{excluded} will exclude the year 40 from "20 to 40",
#' to prevent overlapping age classes. \code{included} is for age ranges like "20 to 39"
#' to prevent overlapping age classes. \code{included} is for age ranges like "20 to 39"
#' where the year 39 is meant to be counted.
#'
#' @return A list of input parameter needed for the function \code{life.table}.
Expand Down Expand Up @@ -72,9 +78,9 @@ NA_to_0 <- function(x) {
#' life.table(magda_prep)
#'
#' @export
prep.life.table=function(x, dec = NA, agebeg, ageend, group = NA, method = "Standard", agerange= "included"){
prep.life.table=function(x, dec = NA, agebeg, ageend = NA, group = NA, method = "Standard", agerange= "included"){

asd <- x
asd <- data.frame(x)

# Ask if "dec" is set / if a count of deceased people exists.
# Otherwise one deceased person is assumed for each row.
Expand All @@ -86,7 +92,11 @@ prep.life.table=function(x, dec = NA, agebeg, ageend, group = NA, method = "Stan

# Change the names of agebeg and ageend for further processes to "beg" and "ende".
names(asd)[which(names(asd)==agebeg)] <- "beg"
names(asd)[which(names(asd)==ageend)] <- "ende"
if (!is.na(ageend)) {
names(asd)[which(names(asd)==ageend)] <- "ende"
} else {
asd$ende <- asd$beg
}

# Filters potential NA values from the begin or end column.
# asd=asd %>% filter(!is.na(beg), !is.na(ende))
Expand Down Expand Up @@ -134,15 +144,19 @@ prep.life.table=function(x, dec = NA, agebeg, ageend, group = NA, method = "Stan
# Set the age values from 0 to the maximum age +1.
restab$Age=seq(0,max(asd$ende,na.rm=T),1)

# For each Group (k) the deaths per age class (available years i) are summed up equally seperated by ages.
# For each Group (k) the deaths per age class (available years i) are summed up equally separated by ages.
for(k in 1:length(unique(asd$Group))){
for(i in which(asd$Group==unique(asd$Group)[k])){
restab[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1)),(k+1)] <- restab[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1)),(k+1)] %+0% (asd$cof[i]/(length(seq(asd$beg[i],asd$ende[i],1))))
restab[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1)),(k+1)] <-
restab[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1)),(k+1)] %+0%
(asd$cof[i]/(length(seq(asd$beg[i],asd$ende[i],1))))
}
}
# Also for all Groups all deceased are summed up seperated according to the years.
# Also for all Groups all deceased are summed up separated according to the years.
for(i in seq_along(asd[,1])){
restab$All[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))] <- (restab$All[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))]) %+0% (asd$cof[i]/(length(seq(asd$beg[i],asd$ende[i],1))))
restab$All[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))] <-
(restab$All[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))]) %+0%
(asd$cof[i]/(length(seq(asd$beg[i],asd$ende[i],1))))
}

# If no groups (male, female, phase, ...) are specified, do the same without considering groups.
Expand All @@ -151,7 +165,9 @@ prep.life.table=function(x, dec = NA, agebeg, ageend, group = NA, method = "Stan
restab=data.frame(Age=seq(0,99,1),Deceased=0)

for(i in seq_along(asd[,1])){
restab$Deceased[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))] <- (restab$Deceased[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))]) %+0% (asd$cof[i]/(length(seq(asd$beg[i],asd$ende[i],1))))
restab$Deceased[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))] <-
(restab$Deceased[is.element(restab$Age,seq(asd$beg[i],asd$ende[i],1))]) %+0%
(asd$cof[i]/(length(seq(asd$beg[i],asd$ende[i],1))))
}
}

Expand Down
4 changes: 2 additions & 2 deletions R/plot_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ make_ggplot <- function(data, variable_name, variable_labels, line_vis) {
colnames(my_x) <- c("a", "variable", variable_name, "dataset")
my_x$a <- unlist(by(my_x$a, my_x$dataset, function(x) cumsum(x))) - my_x$a
if (line_vis == "linetype") {
my_plot <- ggplot2::ggplot(my_x, ggplot2::aes(x="a",y=variable_name, linetype="dataset"))
my_plot <- ggplot2::ggplot(my_x, ggplot2::aes(x=!! rlang::sym("a"),y=!! rlang::sym(variable_name), linetype="dataset"))
} else if (line_vis %in% c("colour", "color")) {
my_plot <- ggplot2::ggplot(my_x, ggplot2::aes(x="a",y=variable_name, color="dataset"))
my_plot <- ggplot2::ggplot(my_x, ggplot2::aes(x=!! rlang::sym("a"),y=!! rlang::sym(variable_name), color="dataset"))
}
my_plot <- my_plot + ggplot2::geom_line() + ggplot2::xlab("age") + ggplot2::ylab(variable_name) + ggplot2::ggtitle(variable_labels[variable_name])
# check if group attribute is present to pass it on for plot legend title
Expand Down
20 changes: 13 additions & 7 deletions man/prep.life.table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4f20272

Please sign in to comment.