Skip to content

Commit a3c859c

Browse files
authored
Merge pull request #5 from lsteinmann/functions
update on get.histogramscale()
2 parents 644172f + 8886590 commit a3c859c

9 files changed

Lines changed: 111 additions & 71 deletions

File tree

R/get.histogramscale.R

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
#' @title Scaling Factor for Combined Histogramm Plots
22
#'
3-
#' @description Requires a dataframe as produced by datsteps().
4-
#' Meaning 6 columns in the following order:
5-
#' * ID,
6-
#' * group,
7-
#' * minimum/earliest date,
8-
#' * maximum/latest date,
9-
#' * weight,
10-
#' * 'DAT_Steps'
3+
#' @description Requires a dataframe as produced by datsteps() or a number as DAT_df_steps. Calculated the value with which the
4+
#' y-axis of a density graph should be multplied in order to be visible in the corresponding histogram.
115
#'
12-
#' @param DAT_df a dataframe as returned by datsteps
13-
#' @param bw the bandwidth to use for the density function and histogram. Should be stepsize used to
14-
#' create the dataframe. If a df as returned by datsteps() is given, stepsize is automatically assigned
15-
#' using the corresponding attribute.
6+
#' @param DAT_df_steps a dataframe as returned by datsteps
7+
#' @param binwidth the bandwidth to use for the density function and histogram. Should be stepsize used to
8+
#' create the dataframe. If a df as returned by datsteps() is given, stepsize can be automatically assigned
9+
#' using the corresponding attribute (binwidth = "stepsize")
1610
#'
1711
#' @return the value with which to scale the density curve to a histogram plot so that both will be visible
1812
#'
1913
#' @examples
2014
#' DAT_df_steps <- datsteps(DAT_df[1:100,], stepsize = 25)
2115
#' get.histogramscale(DAT_df_steps)
2216
#'
17+
#' get.histogramscale(500, binwidth = 20)
18+
#'
2319
#' @export get.histogramscale
2420

25-
get.histogramscale <- function(DAT_df, bw = "auto") {
26-
if (bw == "auto") {
27-
if (!is.null(attributes(DAT_df)$stepsize)) {
28-
bw <- attributes(DAT_df)$stepsize
29-
} else {
30-
stop("Either specify stepsize/bandwidth (bw = ) or use a data.frame as returned by datsteps()")
21+
get.histogramscale <- function(DAT_df_steps, binwidth = "stepsize") {
22+
if (check.number(DAT_df_steps)) {
23+
nrow <- DAT_df_steps
24+
if (binwidth == "stepsize") {
25+
stop("'binwidth == stepsize' cannot be used with a number, supply either a dataframe as returned by datsteps or a numerical bindwidth")
26+
}
27+
} else {
28+
nrow <- nrow(DAT_df_steps)
29+
if (binwidth == "stepsize") {
30+
binwidth <- attributes(DAT_df_steps)$stepsize
31+
if (is.null(binwidth)) {
32+
stop("Supply numerical binwidth or dataframe as returned by datsteps")
33+
}
34+
} else if (!check.number(binwidth)) {
35+
stop('Supply numerical binwidth or use binwidth = "stepsize".')
3136
}
32-
} else if (!is.numeric(bw)) {
33-
stop("Either specify stepsize/bandwidth (bw = ) or use a data.frame as returned by datsteps()")
3437
}
35-
density <- density(DAT_df$DAT_step, weights = DAT_df$weight, bw = bw)
36-
breaks <- range(DAT_df$DAT_step)
37-
breaks <- breaks[2] - breaks [1]
38-
breaks <- round(breaks / bw, digits = 0)
39-
hist <- hist(DAT_df$DAT_step, breaks = breaks)
40-
histogramscale <- max(hist$counts) / max(density$y)
38+
histogramscale <- nrow * binwidth
4139
return(histogramscale)
4240
}

R/scaleweights.R

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
#' @title Scales the content of the weight columns according to group membership
1+
#' @title Scales the content of a column according to group membership
22
#'
3-
#' @description Requires a dataframe as produced by datsteps(). (Meaning 6 columns in the following order: ID, group, minimum/earliest date, maximum/latest date, weight, 'DAT_Steps')
3+
#' @description Requires a dataframe with one variable and one value column.
44
#'
5-
#' @param DAT_df a dataframe as returned by datsteps
6-
#' @param var the index of the column of said dataframe that should be used as the group variable, OR "all" (note: all non-numeric values will result in the weight being scaled accross all objects)
5+
#' @param DAT_df a dataframe
6+
#' @param var the index of the column that should be used as the group variable, OR "all"
7+
#' (note: all non-numeric values will result in the weight being scaled accross all objects)
8+
#' @param val the column that should be scaled (value / sum(values))
79
#'
8-
#' @return the same dataframe, with scaled 'weight'-values
10+
#' @return the same dataframe, with scaled values in the specifies column
911
#'
1012
#' @export scaleweight
1113

12-
scaleweight <- function(DAT_df, var = c("all", 2) ) {
13-
res_DAT_df <- data.frame(NULL)
14-
if (is.numeric(var)) {
15-
uvar <- unique(DAT_df[,var])
16-
for (row in 1:length(uvar)) {
17-
wip <- DAT_df[which(DAT_df[,var] == uvar[row]),]
18-
wip$weight <- wip$weight / sum(wip$weight)
19-
res_DAT_df <- rbind(res_DAT_df, wip)
20-
}
21-
attr(DAT_df$weight, "descr") <- "weight (scaled to sum of objects grouped by variable)"
22-
} else {
23-
DAT_df$weight <- DAT_df$weight / sum(DAT_df$weight)
24-
attr(DAT_df$weight, "descr") <- "weight (scaled to sum of all objects)"
25-
res_DAT_df <- DAT_df
14+
scaleweight <- function(DAT_df, var = c("all", 2), val = 5 ) {
15+
if (check.number(var)) {
16+
if (check.number(val)) {
17+
uvar <- unique(DAT_df[,var])
18+
for (row in 1:length(uvar)) {
19+
index <- which(DAT_df[,var] == uvar[row])
20+
DAT_df[index,val] <- DAT_df[index,val] / sum(DAT_df[index,val])
21+
}
22+
attr(DAT_df[,val], "descr") <- "weight (scaled to sum of objects grouped by variable)"
23+
} else { stop("val needs to be of a number (the index of the column that should be scaled)")}
24+
} else {
25+
DAT_df[,val] <- DAT_df[,val] / sum(DAT_df[,val])
26+
attr(DAT_df[,val], "descr") <- "weight (scaled to sum of all objects)"
2627
}
27-
28-
return(res_DAT_df)
28+
return(DAT_df)
2929
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!-- README.md is generated from README.Rmd. Please edit that file -->
22
<!-- badges: start -->
33
[![R build status](https://github.com/lsteinmann/datplot/workflows/R-CMD-check/badge.svg)](https://github.com/lsteinmann/datplot/actions)
4-
[![Codecov test coverage](https://codecov.io/gh/lsteinmann/datplot/branch/master/graph/badge.svg)](https://codecov.io/gh/lsteinmann/datplot?branch=master)
54
[![Travis build status](https://travis-ci.com/lsteinmann/datplot.svg?branch=master)](https://travis-ci.com/lsteinmann/datplot)
5+
[![Codecov test coverage](https://codecov.io/gh/lsteinmann/datplot/branch/master/graph/badge.svg)](https://codecov.io/gh/lsteinmann/datplot?branch=master)
66
<!-- badges: end -->
77

88
datplot

man/get.histogramscale.Rd

Lines changed: 9 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scaleweight.Rd

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-datplot_utility.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ test_that("check.number returns true for numbers false for other", {
5555
expect_false(check.number(testdf_wrong[3,3]))
5656
expect_true(check.number(testdf_wrong[3,4]))
5757
expect_false(check.number(testdf_wrong_two[3,4]))
58+
expect_false(check.number(testdf))
5859
})
5960

6061

@@ -68,3 +69,7 @@ test_that("check.structure works", {
6869

6970

7071

72+
test_that("check.structure issues warning", {
73+
expect_warning(check.structure(testdf), regexp = "recommended")
74+
})
75+

tests/testthat/test-datsteps.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ source(file = "../create_testing_df.R")
22
testdf <- create.testing.df()
33

44
test_that("error for wrong value of stepsize", {
5-
expect_error(datsteps(testdf, stepsize = "test"))
5+
expect_error(datsteps(testdf, stepsize = "test"), "stepsize has to be")
66
expect_warning(datsteps(testdf, stepsize = 25))
77
})
8+
9+
data("DAT_df")
10+
11+
str(DAT_df)
12+
13+
test_that("error for wrong value of stepsize", {
14+
expect_warning(datsteps(DAT_df), regexp = "wrong order")
15+
16+
})

tests/testthat/test-get.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ attributes(teststeps_wrong)$stepsize <- NULL
99
#str(teststeps_wrong)
1010

1111

12-
test_that("multiplication works", {
13-
#expect_type(get.histogramscale(teststeps), c("numeric", "double", "integer"))
14-
expect_error(get.histogramscale(teststeps_wrong))
12+
13+
test_that("right errors are returned", {
14+
expect_error(get.histogramscale(teststeps_wrong, binwidth = "stepsize"), regexp = "dataframe as returned by datsteps")
15+
expect_error(get.histogramscale(teststeps_wrong), regexp = "dataframe as returned by datsteps")
16+
expect_error(get.histogramscale(testdfsteps, binwidth = "börek"), regexp = "or use")
17+
expect_error(get.histogramscale(20), regexp = "cannot be used with a number")
18+
})
19+
20+
21+
test_that("returns number", {
22+
expect_true(check.number(get.histogramscale(testdfsteps)))
23+
expect_true(check.number(get.histogramscale(20, binwidth = 5)))
1524
})

tests/testthat/test-scaleweights.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
source(file = "../create_testing_df.R")
2+
3+
4+
testdf <- create.testing.df()
5+
6+
testdf_steps <- datsteps(testdf)
7+
8+
9+
test_that("scaleweight appends attribute", {
10+
expect_equal(2 * 2, 4)
11+
expect_match(as.character(attributes(scaleweight(testdf_steps, var = "all")$weight)), regexp = "all objects")
12+
expect_match(as.character(attributes(scaleweight(testdf_steps, var = 2)$weight)), regexp = "by variable")
13+
})
14+
15+
16+
test_that("val accepts only numbers", {
17+
expect_error(scaleweight(testdf_steps, val = "no"))
18+
expect_error(scaleweight(testdf_steps, val = 8))
19+
})
20+

0 commit comments

Comments
 (0)