Shardul Pendharkar
Shardul Pendharkar

Reputation: 137

rtruncnorm function in R generates same observations

I am trying to use truncated normal distribution function in R to generate numbers between upper and lower bounds. This is the statement I am using:

rtruncnorm(4,1494396000,1494397800, 6360,2640)

4 - Number of observations i need, 1494396000 - Epoch time (actual time: 2017-05-10 02:00:00), 1494397800 - Epoch time (actual time: 2017-05-10 02:30:00), 6360 - mean 106 mins (106*60 = 6360 seconds), 2640 - SD 44 mins (44*60 = 2640 seconds)

This should ideally give me 4 observations of epoch times between 02:00:00 and 02:30:00 on May 10 But the output I am getting is: 1494396000 1494396000 1494396000 1494396000 which is 2017-05-10 02:00:00

I can't understand why rtruncnorm is giving me 4 exactly same observations. I tried using rtnorm function from msm package and the result is the same.

A nudge in the right direction would be greatly appreciated

Upvotes: 1

Views: 2467

Answers (2)

HFBrowning
HFBrowning

Reputation: 2336

(Eipi10 beat me to the punch, and this answer is meant to be a complement to theirs which explains why R behaves the way it does)

What you're essentially attempting to do is to pull values from the extreme end of a tail of a distribution with a standard deviation that's also very large relative to your limits. I have made a horrible quick picture in MSPaint to help demonstrate.

enter image description here

Given that it sounds like you just want some random values in between an upper and lower bound - are you sure you need to define a mean and standard deviation (or even know what those might be?) If you don't need to model that you'd be better off just using:

runif(4, min = 1494396000, max = 1494397800)

Upvotes: 1

eipi10
eipi10

Reputation: 93851

The lower truncation limit of 1,494,396,000 is 566,057 standard deviations above the mean of 6,360. It looks like when you specify a mean that results in all the simulated values being below the lower truncation limit, the function just returns the lower truncation limit for every value. I think you must have meant to specify a mean somewhere between your truncation limits.

library(truncnorm)

rr = c(1494396000, 1494397800)

hist(rtruncnorm(10000, 1494396000, 1494397800, 1494396000, 500), 
     breaks=seq(rr[1],rr[2], length=40), xlim=rr, main="Mean=1494396000", xlab="")

hist(rtruncnorm(10000, 1494396000, 1494397800, 6360, 500), 
     breaks=seq(rr[1],rr[2], length=40), xlim=rr, main="Mean=6360", xlab="")

enter image description here

Upvotes: 1

Related Questions