# Why is hist not defined after loading StatsBase?

**URL:** <https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 13, 2018, 8:17am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868 "2018-05-13T08:17:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [May 13, 2018, 8:17am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/1 "2018-05-13T08:17:04Z")

</div>

So this might be a really stupid question and I have the feeling that it will be. However, I have to ask it because it doesn’t make sense to me.

I have installed and loaded julia v 0.6.2 on my ubuntu linux 17.10 box. I run the following code in the REPL

```julia
using StatsBase
?hist

```

and I get this as output

> Couldn’t find **hist**  
> Perhaps you meant **hist** , Chisq, hash, hcat, FDist, TDist, fit, Cint or Dict  
> No documentation found.
> 
> Binding **hist** does not exist.

So it’s telling me **hist** couldn’t be found but instead recommends to me that I might be looking for **hist**?

Follow up question:

I’m trying to achieve the julia version of the following executed in R

```r
plot(hist(rnorm(100)))

```

![hist](https://global.discourse-cdn.com/julialang/original/3X/2/6/26fca86d31a7a321219694d88940579c6a86ff49.png)

What is the Julia way of achieving this?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 13, 2018, 8:53am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/2 "2018-05-13T08:53:05Z")

</div>

> [@DoktorMike](#):
>
> So it’s telling me hist couldn’t be found but instead recommends to me that I might be looking for hist?

I think this is because `hist` is exported by `StatsBase`, but not defined within the module.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2018, 8:55am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/3 "2018-05-13T08:55:18Z")

</div>

In StatsBase you use `fit(Histogram, randn(100))` but this does not produce the actual plot (only calculates the bins and weights).

The simplest way to get the plot is:

```julia
using StatPlots
histogram(randn(100))

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2018, 8:56am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/4 "2018-05-13T08:56:39Z")

</div>

AFAIK `hist` was removed from `StatsBase` (`fit` should be used).

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [May 13, 2018, 8:57am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/5 "2018-05-13T08:57:16Z")

</div>

Thank you. I’ll give that a whirl!

So if this is the way to get a Histogram then what does the hist function do? Is it something completely different?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 13, 2018, 8:59am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/6 "2018-05-13T08:59:52Z")

</div>

Right, I just meant that the [`export hist`](https://github.com/JuliaStats/StatsBase.jl/blob/cecb88fac7fbe14bbd34e57d5450fe57d0890b28/src/StatsBase.jl#L130) should be removed 🙂

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2018, 9:01am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/7 "2018-05-13T09:01:24Z")

</div>

right - I misread your post 😄

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2018, 9:02am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/8 "2018-05-13T09:02:54Z")

</div>

It gives you a histogram data but does not plot it:

```julia
julia> fit(Histogram, randn(100))
StatsBase.Histogram{Int64,1,Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}
edges:
  -3.0:1.0:4.0
weights: [3, 11, 39, 33, 11, 2, 1]
closed: left
isdensity: false

```

(and `hist` export in `StatsBase` is a bug that should be fixed as @fredrikekre indicated)

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [May 13, 2018, 9:04am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/9 "2018-05-13T09:04:40Z")

</div>

Thanks for the help. 👍

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 13, 2018, 9:39am UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/10 "2018-05-13T09:39:18Z")

</div>

To be exact, you just need Plots (StatPlots is a superset of Plots that includes DataFrames support and some extra recipes useful for plotting with data; but for most users it’s recommended to just use StatPlots rather than Plots anyway).  
If you do want to fit a histogram with StatsBase and then plot it, you can do that too.

```julia
using Plots, StatsBase
h = fit(Histogram, randn(10000))
plot(h)

```

But the Plots `histogram` call is slightly more featured in that you can use `:scott`, `:rice`, `:fd` (Friedman-Diaconis, the default) and `:sturges` algorithms to get the bin breaks (StatPlots also offers `:wand` which gives a better fit to a density function but does not align breaks neatly with integer values). StatsBase only uses `:sturges`

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [May 13, 2018, 1:35pm UTC](https://discourse.julialang.org/t/why-is-hist-not-defined-after-loading-statsbase/10868/11 "2018-05-13T13:35:41Z")

</div>

Very informative. Thanks for the explanation. Indeed I’m searching for the differences in Julian vs R thinking. After 20 years of C++ and R I’m a little set in my ways. Loving what I’ve seen so far though.
