Skip to content

Commit

Permalink
Merge pull request #4 from JavierMtzRdz/develop
Browse files Browse the repository at this point in the history
Update notes
  • Loading branch information
JavierMtzRdz authored Dec 21, 2023
2 parents cfe69c6 + e3c1ba7 commit c1de636
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 9 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^README\.Rmd$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
.Rproj.user
.Rproj
inst/
misc/
^tidydelta\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: tidydelta
Title: Estimation of standard errors using Delta Method.
Title: Estimation of standard errors using Delta Method
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
person("Javier", "Martinez-Rodriguez", , "[email protected]",
role = c("aut", "cre"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Imports:
Expand All @@ -12,6 +12,8 @@ Imports:
purrr,
rlang,
tibble
Suggests:
tidyverse
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright (c) 2023 tidydelta authors
Copyright (c) 2023 Javier Martínez-Rodríguez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 23 additions & 4 deletions R/tidydelta.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,41 @@ ext_bd_var <- function(formula) {
}


#' Estimate means and standard errors
#' Delta Method implementation
#'
#' Estimates means and standard errors for a given formula using Monte Carlo simulation.
#' Estimates standard errors for transformations of random variables using Delta method.
#'
#' @param formula A formula object specifying the variables of interest.
#' @param normality_eval Logical value to run normality test in case of being
#' possible.
#' @param formula_vars The function(s) to apply to the variables in the formula.
#' @param mean_dta Data frame containing the means of the variables.
#' @param mean_dta Vector containing the means of the variables.
#' @param cov_dta Covariance matrix of the variables.
#' @param conf_lev Confidence level for confidence intervals.
#' @param n Sample size evaluate (in case that we can evaluate the confidence
#' @param n Sample size evaluation (in case that we can evaluate the confidence
#' intervals with different hypnotic sample sizes).
#'
#' @return A tibble with columns for means, standard errors, and optionally, confidence intervals.
#'
#' @examples
#' # Equivalent ways to use tidydelta_m()
#' library(tidyverse)
#'
#' x = rnorm(1000, mean = 5, sd = 2)
#' y = rnorm(1000, mean = 15, sd = 3)
#'
#' bd <- tibble(x, y)
#'
#' tidydelta_m(~ y/x,
#' conf_lev = .95)
#'
#' tidydelta_m(~ bd$y/bd$x,
#' conf_lev = .95)
#' bd %>%
#' summarise(tidydelta_m(~ y/x,
#' conf_lev = .95))
#'
#'
#' @export
tidydelta_m <- function(
formula,
Expand Down
80 changes: 80 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# tidydelta

<!-- badges: start -->
<!-- badges: end -->

Delta Method implementation to estimate standard errors in a {tidyverse} workflow.

## Installation

You can install the development version of tidydelta from [GitHub](https://github.com/JavierMtzRdz/tidydelta) with:

``` r
remotes::install_github("JavierMtzRdz/tidydelta")
# Or
devtools::install_github("JavierMtzRdz/tidydelta")
```

## Example

Using tidydelta_m(), the following commands are equivalent:

```{r example}
# Load packages
library(tidydelta)
library(tidyverse)
# Simulate samples
set.seed(547)
x = rnorm(10000, mean = 5, sd = 2)
y = rnorm(10000, mean = 15, sd = 3)
bd <- tibble(x, y)
# Equivalent uses of tidydelta_m()
tidydelta_m(~ y/x,
conf_lev = .95)
tidydelta_m(~ bd$y/bd$x,
conf_lev = .95)
bd %>%
summarise(tidydelta_m(~ y/x,
conf_lev = .95))
```

Now, the data frame is divided into samples to compare the transformation of the sample with the estimation of `tidydelta()`. In the real world, you would not need to compute the Delta Method if you have many samples, but it shows how it can be incorporated in a workflow with tidyverse.

```{r message=FALSE, warning=FALSE, fig.height=3.5, dpi=300}
(result <- bd %>%
summarise(tidydelta_m(~ x/y,
conf_lev = .95)))
ggplot() +
geom_histogram(data = bd %>%
mutate(t = x/y),
aes(x = t)) +
geom_vline(aes(xintercept = result$T_n,
color = "T_n")) +
geom_vline(aes(xintercept = c(result$lower_ci,
result$upper_ci),
color = "CI"),
linetype = "dashed") +
labs(color = element_blank()) +
theme_minimal() +
theme(text = element_text(family = "Times New Roman"))
```
105 changes: 104 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,104 @@
# stat547c_topics-in-probability

<!-- README.md is generated from README.Rmd. Please edit that file -->

# tidydelta

<!-- badges: start -->
<!-- badges: end -->

Delta Method implementation to estimate standard errors in a {tidyverse}
workflow.

## Installation

You can install the development version of tidydelta from
[GitHub](https://github.com/JavierMtzRdz/tidydelta) with:

``` r
remotes::install_github("JavierMtzRdz/tidydelta")
# Or
devtools::install_github("JavierMtzRdz/tidydelta")
```

## Example

Using tidydelta_m(), the following commands are equivalent:

``` r
# Load packages
library(tidydelta)
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 4.3.1
#> Warning: package 'stringr' was built under R version 4.3.1
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#> ✔ dplyr 1.1.4 ✔ readr 2.1.4
#> ✔ forcats 1.0.0 ✔ stringr 1.5.1
#> ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
#> ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
#> ✔ purrr 1.0.2
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

# Simulate samples
set.seed(547)
x = rnorm(10000, mean = 5, sd = 2)
y = rnorm(10000, mean = 15, sd = 3)

bd <- tibble(x, y)

# Equivalent uses of tidydelta_m()
tidydelta_m(~ y/x,
conf_lev = .95)
#> # A tibble: 1 × 6
#> y x T_n se lower_ci upper_ci
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 15.0 5.02 2.99 1.33 0.378 5.61

tidydelta_m(~ bd$y/bd$x,
conf_lev = .95)
#> # A tibble: 1 × 6
#> y x T_n se lower_ci upper_ci
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 15.0 5.02 2.99 1.33 0.378 5.61
bd %>%
summarise(tidydelta_m(~ y/x,
conf_lev = .95))
#> # A tibble: 1 × 6
#> y x T_n se lower_ci upper_ci
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 15.0 5.02 2.99 1.33 0.378 5.61
```

Now, the data frame is divided into samples to compare the
transformation of the sample with the estimation of `tidydelta()`. In
the real world, you would not need to compute the Delta Method if you
have many samples, but it shows how it can be incorporated in a workflow
with tidyverse.

``` r
(result <- bd %>%
summarise(tidydelta_m(~ x/y,
conf_lev = .95)))
#> # A tibble: 1 × 6
#> x y T_n se lower_ci upper_ci
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 5.02 15.0 0.334 0.149 0.0422 0.626

ggplot() +
geom_histogram(data = bd %>%
mutate(t = x/y),
aes(x = t)) +
geom_vline(aes(xintercept = result$T_n,
color = "T_n")) +
geom_vline(aes(xintercept = c(result$lower_ci,
result$upper_ci),
color = "CI"),
linetype = "dashed") +
labs(color = element_blank()) +
theme_minimal() +
theme(text = element_text(family = "Times New Roman"))
```

<img src="man/figures/README-unnamed-chunk-2-1.png" width="100%" />
Binary file added man/figures/README-unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions man/tidydelta_m.Rd

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

0 comments on commit c1de636

Please sign in to comment.