Ecto.OLAP v0.2.1 Ecto.OLAP.Statistics View Source
Set of macros providing additional aggregates for statistics counting.
Example data
All examples assumes we have table stats_agg with content:
| year | divorce_rate | marg_cons |
|---|---|---|
| 2000 | 5.0 | 8.2 |
| 2001 | 4.7 | 7.0 |
| 2002 | 4.6 | 6.5 |
| 2003 | 4.4 | 5.3 |
| 2004 | 4.3 | 5.2 |
| 2005 | 4.1 | 4.0 |
| 2006 | 4.2 | 4.6 |
| 2007 | 4.2 | 4.5 |
| 2008 | 4.2 | 4.2 |
| 2009 | 4.2 | 3.7 |
Example data thanks to Tyler Vigen’s Spurious Correlations
Link to this section Summary
Functions
Compute correlation coefficient for given y and x expressions
Average of the independent variable x
Average of the dependent variable y
Count rows where both expressions are nonnull
y-intercept of the least-squares-fit linear equation determined
by the (x, y) pairs
Square of the correlation coefficient
Slope of the least-squares-fit linear equation determined
by the (x, y) pairs
“Sum of squares” of independent variable
“Sum of products” of independent times dependent variable
“Sum of squares” of dependent variable
Link to this section Functions
Compute correlation coefficient for given y and x expressions.
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: corr(e.divorce_rate, e.marg_cons)
0.9806576205544681
Average of the independent variable x.
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_avgx(e.divorce_rate, e.marg_cons)
5.320000000000001
Average of the dependent variable y.
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_avgy(e.divorce_rate, e.marg_cons)
4.390000000000001
Count rows where both expressions are nonnull.
y-intercept of the least-squares-fit linear equation determined
by the (x, y) pairs.
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_intercept(e.divorce_rate, e.marg_cons)
3.363198179561455
Square of the correlation coefficient.
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_r2(e.divorce_rate, e.marg_cons)
0.9616893687515513
Slope of the least-squares-fit linear equation determined
by the (x, y) pairs.
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_slope(e.divorce_rate, e.marg_cons)
0.1930078609846896
“Sum of squares” of independent variable.
sum(x^2) - sum(x)^2
-------------------
N
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_sxx(e.divorce_rate, e.marg_cons)
19.33599999999983
“Sum of products” of independent times dependent variable.
sum(x * y) - sum(x) * sum(y)
----------------------------
N
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_sxy(e.divorce_rate, e.marg_cons)
3.7319999999999256
“Sum of squares” of dependent variable.
sum(y^2) - sum(y)^2
-------------------
N
Example
iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...> select: regr_syy(e.divorce_rate, e.marg_cons)
0.7489999999999327