knitr::opts_chunk$set( error = TRUE, collapse = TRUE, comment = "#>", out.width = "100%" )
library(testthat) library(microbenchmark) library(jeksterslabRdist) context("Test Normal - PDF.")
n <- 50 mu <- runif( n = 1, min = 1, max = 2 ) sigma <- runif( n = 1, min = 1, max = 2 ) Variable <- c( "`n`", "`mu`", "`sigma`" ) Description <- c( "Sample size ($n$).", "Population mean ($\\mu$).", "Population standard deviation ($\\sigma$)." ) Value <- c( n, mu, sigma ) knitr::kable( x = data.frame( Variable, Description, Value ), row.names = FALSE )
x <- rnorm( n = n, mean = mu, sd = sigma ) x_bar <- mean(x) s <- sd(x) Variable <- c( "`n`", "`x_bar`", "`s`" ) Description <- c( "Sample size ($n$).", "Samnple mean ($\\bar{x}$).", "Sample standard deviation ($s$)." ) Value <- c( n, x_bar, s ) knitr::kable( x = data.frame( Variable, Description, Value ), row.names = FALSE )
results_dnorm <- dnorm( x = x, mean = mu, sd = sigma, log = FALSE ) results_normpdf <- normpdf( x = x, mu = mu, sigma = sigma, log = FALSE ) results_dnorm_log <- dnorm( x = x, mean = mu, sd = sigma, log = TRUE ) results_normpdf_log <- normpdf( x = x, mu = mu, sigma = sigma, log = TRUE )
plot( x = x, y = results_normpdf )
plot( x = x, y = results_dnorm )
plot( x = x, y = results_normpdf_log )
plot( x = x, y = results_dnorm_log )
knitr::kable( x = data.frame( x = x, dnorm = results_dnorm, normpdf = results_normpdf, dnorm_log = results_dnorm_log, normpdf_log = results_normpdf_log ), row.names = FALSE )
microbenchmark( dnorm = dnorm(x = x, mean = mu, sd = sigma, log = FALSE), normpdf = normpdf(x = x, mu = mu, sigma = sigma, log = FALSE), dnorm_log = dnorm(x = x, mean = mu, sd = sigma, log = TRUE), normpdf_log = normpdf(x = x, mu = mu, sigma = sigma, log = TRUE) )
test_that("normpdf return the same values as dnorm", { expect_equivalent( round( x = results_dnorm, digits = 2 ), round( x = results_normpdf, digits = 2 ) ) })
test_that("normpdf log = TRUE return the same values as dnorm log = TRUE", { expect_equivalent( round( x = results_dnorm_log, digits = 2 ), round( x = results_normpdf_log, digits = 2 ) ) })
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.