Giter Club home page Giter Club logo

Comments (1)

jennybc avatar jennybc commented on June 14, 2024 1

My archaeological dig has yielded a really simple version of rthis(). It implements a smooth bootstrap with Gaussian kernel. It's described many places, including by Silverman in Density Estimation for Statistics and Data Analysis (1986) in section 6.4.1 "Simulating from density estimates". I included a bit of visual and numerical exploration as a sanity check.

library(ggplot2)
library(tibble)
library(purrr)
library(dplyr)
library(tidyr)

rthis <- function(n, data, h) {
  if (missing(n)) {
    n <- length(data)
  }
  if (missing(h)) {
    h <- bw.nrd(data) ## or use one of the other bw selectors??
  }
  xbar <- mean(data)
  sigsq <- var(data)
  ystar <- sample(data - xbar, size = n, replace = TRUE)
  epsilon <- rnorm(n)
  denom <- sqrt(1 + h^2 / sigsq)
  xbar + (ystar + h * epsilon) / denom
}

## try it with old faithful data
fdur <- faithful$eruptions

B <- 100
## can also play with h like so: h = bw.nrd(fdur) * 1.3)
df <- rerun(B, rthis(data = fdur)) %>%
  set_names(as.character(seq_len(B))) %>%
  as_tibble() %>%
  add_column(fdur, .before = 1) %>%
  gather(key = "src", value = "duration")

for_gg <- df %>%
  filter(src %in% c("fdur", as.character(1:5)))
ggplot(for_gg, aes(duration)) + geom_freqpoly() +
  geom_rug() + facet_wrap(~ src)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

summ_stats <- df %>%
  filter(src != "fdur") %>%
  group_by(src) %>%
  summarise(mean = mean(duration), var = var(duration))
tribble(
   ~stat,    ~actual,           ~sample_avg,
  "mean", mean(fdur), mean(summ_stats$mean),
   "var",  var(fdur),  mean(summ_stats$var)
)
#> # A tibble: 2 × 3
#>    stat   actual sample_avg
#>   <chr>    <dbl>      <dbl>
#> 1  mean 3.487783   3.495127
#> 2   var 1.302728   1.312251

from funs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.