Skip to content

Commit

Permalink
added random seed in tests
Browse files Browse the repository at this point in the history
to avoid accidental failing of the tests
  • Loading branch information
bgctw committed Jan 4, 2021
1 parent 53631f7 commit ae29c40
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 76 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Suggests: RUnit, knitr, ggplot2, reshape2
VignetteBuilder: knitr
License: GPL-2
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 7.1.1
4 changes: 3 additions & 1 deletion inst/unitTests/runitLogitnorm.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ test.plogitnorm <- function(){

test.dlogitnorm <- function(){
q <- c(-1,0,0.5,1,2)
set.seed(0815)
ans <- suppressWarnings(dlogitnorm(q))
checkEquals(c(0,0,1.595769,0,0), ans, tolerance = 1e-7)
}
Expand Down Expand Up @@ -80,7 +81,8 @@ test.twCoefLogitnormMLE <- function(){


test.twCoefLogitnormE <- function(){
theta <- twCoefLogitnormE(0.7,0.9)
set.seed(0815)
theta <- twCoefLogitnormE(0.7,0.9)
px <- plogitnorm(x,mu = theta[1],sigma = theta[2]) #percentiles function
dx <- dlogitnorm(x,mu = theta[1],sigma = theta[2]) #density function
#plot(px~x); abline(v = c(0.7,0.9)); abline(h = c(0.5,0.975))
Expand Down
88 changes: 45 additions & 43 deletions inst/unitTests/runitmodeLogitnorm.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
#TODO

.setUp <-function () {
}

.tearDown <- function () {
}

test.rightMode <- function(){
theta0 <- c(mu=1.5, sigma=0.8)
#plot the true and the rediscovered distributions
xGrid = seq(0,1, length.out=81)[-c(1,81)]
dx <- dlogitnorm(xGrid, mu=theta0[1], sigma=theta0[2])
plot( dx~xGrid, type="l")

mle <- modeLogitnorm(mu=theta0[1], sigma=theta0[2] )
abline(v=mle,col="gray")

#check by monte carlo integration
#z <- rlogitnorm(1e6, mu=theta0[1], sigma=theta0[2]); var(z)
#dz <- density(z)
#checkEqualsNumeric( dz$x[which.max(dz$y)], mle, tolerance=5e-2)
checkEqualsNumeric( 0.88, mle, tolerance=1e-2)
}

test.leftMode <- function(){
theta0 <- c(mu=-1.5, sigma=0.8)
#plot the true and the rediscovered distributions
xGrid = seq(0,1, length.out=81)[-c(1,81)]
dx <- dlogitnorm(xGrid, mu=theta0[1], sigma=theta0[2])
plot( dx~xGrid, type="l")

mle <- modeLogitnorm(mu=theta0[1], sigma=theta0[2] )
abline(v=mle,col="gray")

#check by monte carlo integration
# deprecated: did not run on Windows
#z <- rlogitnorm(1e6, mu=theta0[1], sigma=theta0[2]); var(z)
#dz <- density(z)
#checkEqualsNumeric( dz$x[which.max(dz$y)], mle, tolerance=5e-2)
checkEqualsNumeric( 0.12, mle, tolerance=1e-2) # regression 0.12 calculated previously
}

#TODO

.setUp <-function () {
}

.tearDown <- function () {
}

test.rightMode <- function(){
theta0 <- c(mu=1.5, sigma=0.8)
#plot the true and the rediscovered distributions
xGrid = seq(0,1, length.out=81)[-c(1,81)]
dx <- dlogitnorm(xGrid, mu=theta0[1], sigma=theta0[2])
plot( dx~xGrid, type="l")

set.seed(0815)
mle <- modeLogitnorm(mu=theta0[1], sigma=theta0[2] )
abline(v=mle,col="gray")

#check by monte carlo integration
#z <- rlogitnorm(1e6, mu=theta0[1], sigma=theta0[2]); var(z)
#dz <- density(z)
#checkEqualsNumeric( dz$x[which.max(dz$y)], mle, tolerance=5e-2)
checkEqualsNumeric( 0.88, mle, tolerance=1e-2)
}

test.leftMode <- function(){
theta0 <- c(mu=-1.5, sigma=0.8)
#plot the true and the rediscovered distributions
xGrid = seq(0,1, length.out=81)[-c(1,81)]
dx <- dlogitnorm(xGrid, mu=theta0[1], sigma=theta0[2])
plot( dx~xGrid, type="l")

set.seed(0815)
mle <- modeLogitnorm(mu=theta0[1], sigma=theta0[2] )
abline(v=mle,col="gray")

#check by monte carlo integration
# deprecated: did not run on Windows
#z <- rlogitnorm(1e6, mu=theta0[1], sigma=theta0[2]); var(z)
#dz <- density(z)
#checkEqualsNumeric( dz$x[which.max(dz$y)], mle, tolerance=5e-2)
checkEqualsNumeric( 0.12, mle, tolerance=1e-2) # regression 0.12 calculated previously
}

64 changes: 33 additions & 31 deletions inst/unitTests/runitmomentsLogitnorm.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
#TODO

.setUp <-function () {
}

.tearDown <- function () {
}

test.1 <- function(){
theta0 <- c(mu=1.5, sigma=0.8)
#plot the true and the rediscovered distributions
xGrid = seq(0,1, length.out=81)[-c(1,81)]
dx <- dlogitnorm(xGrid, mu=theta0[1], sigma=theta0[2])
plot( dx~xGrid, type="l")

moments <- momentsLogitnorm(mu=theta0[1], sigma=theta0[2] )
#check by monte carlo integration
z <- rlogitnorm(1e6, mu=theta0[1], sigma=theta0[2]); var(z)
checkEqualsNumeric( mean(z), moments["mean"], tolerance=1e-3)
checkEqualsNumeric( var(z), moments["var"], tolerance=6e-3)
}

test.momentsLogitnorm41 <- function(){
(res <- momentsLogitnorm(4,1))
checkEqualsNumeric( c(0.97189602, 0.00101663), res)
}

test.momentsLogitnorm501 <- function(){
(res <- momentsLogitnorm(5,0.1))
checkEqualsNumeric( c(9.932743e-01, 4.484069e-07), res, tolerance=1e-7)
}
#TODO

.setUp <-function () {
}

.tearDown <- function () {
}

test.1 <- function(){
set.seed(0815)
theta0 <- c(mu=1.5, sigma=0.8)
#plot the true and the rediscovered distributions
xGrid = seq(0,1, length.out=81)[-c(1,81)]
dx <- dlogitnorm(xGrid, mu=theta0[1], sigma=theta0[2])
plot( dx~xGrid, type="l")

moments <- momentsLogitnorm(mu=theta0[1], sigma=theta0[2] )
#check by monte carlo integration
z <- rlogitnorm(1e6, mu=theta0[1], sigma=theta0[2]); var(z)
checkEqualsNumeric( mean(z), moments["mean"], tolerance=1e-3)
checkEqualsNumeric( var(z), moments["var"], tolerance=1e-2)
}

test.momentsLogitnorm41 <- function(){
(res <- momentsLogitnorm(4,1))
checkEqualsNumeric( c(0.97189602, 0.00101663), res)
}

test.momentsLogitnorm501 <- function(){
set.seed(0815)
(res <- momentsLogitnorm(5,0.1))
checkEqualsNumeric( c(9.932743e-01, 4.484069e-07), res, tolerance=1e-7)
}

0 comments on commit ae29c40

Please sign in to comment.