[R] Oddd results when computing confidence intervals

Abraham Mathew mathewanalytics at gmail.com
Fri Dec 2 14:38:43 CET 2016


I have a vector of values, and have written a function that takes each
value in that vector, generates a normal distribution with that value as
the mean, and then finds the interval at different levels. However, these
intervals don't seem to be right (too narrow).

### CREATE PREDICTION INTERVALS

ensemble_forecast = c(200,600,400,500,200,100,200,600,400,500,200,100)
forecast_for=12

lo_intervals = c()
hi_intervals = c()

create_prediction_intervals <- function(use_forecast = ensemble_forecast,
                                        conf_level = 0.90,
                                        do_jitter = FALSE){

   conf.levels1 = paste(round(rep(conf_level, forecast_for/2), 2), "0",
sep="")
   conf.levels2 = seq((conf_level+0.02), 0.95, length=forecast_for/2)
   all.conf.levels = c(conf.levels1, conf.levels2)
   all.conf.levels = as.numeric(as.character(all.conf.levels))
   all.conf.levels

   # forc_num=1
   for(forc_num in 1:length(use_forecast)){
         message("Executing forecast number: ", forc_num, " at confidence
level: ", all.conf.levels[forc_num])

         value = rnorm(5000, mean=use_forecast[forc_num],
sd=sd(use_forecast))

         #t.test(value)$conf.int
         #Rmisc::CI(value, ci=0.99)

         low = Rmisc::CI(value, ci=all.conf.levels[forc_num])[[3]]
         high = Rmisc::CI(value, ci=all.conf.levels[forc_num])[[1]]

         #low = t.test(value, conf.level=all.conf.levels[forc_num])$conf.int
[[1]]
         #high = t.test(value, conf.level=all.conf.levels[forc_num])$
conf.int[[2]]

         lo_intervals.tmp <- c(low)
         hi_intervals.tmp <- c(high)

         if(do_jitter){
            if(length(unique(lo_intervals)) <= 3) lo_intervals.tmp <-
round(jitter(lo_intervals.tmp), 0)
            if(length(unique(hi_intervals)) <= 3) hi_intervals.tmp <-
round(jitter(hi_intervals.tmp), 0)
            lo_intervals <<- c(lo_intervals, lo_intervals.tmp)
            hi_intervals <<- c(hi_intervals, hi_intervals.tmp)
         } else {
            lo_intervals <<- c(lo_intervals, lo_intervals.tmp)
            hi_intervals <<- c(hi_intervals, hi_intervals.tmp)
         }
   }
}

summary(value)
hist(value)
create_prediction_intervals(ensemble_forecast)


Any ideas on what I'm doing wrong?


-- 


*Abraham MathewData Ninja and Statistical Modeler*



*Minneapolis, MN720-648-0108 at abmathewksAnalytics_Blog
<https://mathewanalytics.wordpress.com/>*

	[[alternative HTML version deleted]]



More information about the R-help mailing list