The new Dataset in Mathematica 10 is a great addition to the language, potentially reducing the convenience gap with R. Given this, I would like to understand what the equivalent simple or natural workflow in Mathematica is for a model fit.
In R, one can set up a data frame by columns (c
creates a vector):
dataset=data.frame(days=c(1,2,6,8),area=c(3,6,8,2),frequency=c(1,4,4,2),height=c(2,3,11,6))
which is displayed like this:
days area frequency height
1 1 3 1 2
2 2 6 4 3
3 6 8 4 11
4 8 2 2 6
You can then fit a model using a model formula:
fit=lm(height~days+area,data=dataset)
You could view the coefficients, for example, with coef(fit)
(Intercept) days area
-2.8167116 0.9198113 0.9278976
In Mathematica one option that occurs to me is:
dataset = Dataset[MapThread[Association, Thread /@ {"days" -> {1, 2, 6, 8},
"area" -> {3, 6, 8, 2}, "frequency" -> {1, 4, 4, 2}, "height" -> {2, 3, 11, 6}}]]
LinearModelFit[dataset[[All, {"days", "area", "height"}]] // Normal // Values,
{days, area}, {days, area}]
The specific questions are:
- What is a more natural way to create the Dataset in Mathematica? I find it helpful to have the data specified by columns and for the column name to be near the data, but if that idiom is not natural in Mathematica, that would be good to know.
- Is there a simpler way to do the fit, perhaps one as straightforward as the R approach? Specifying the column names is important for experimentation and model selection when the number of potential variables is large.
Given some comments below, here are some clarifications:
- In R the data frame is a core structure and it is manipulated in many ways e.g. by combining data frames, adding new columns, modifying existing columns etc. My underlying interest is to understand to what extent Dataset can play this role. I think the column view is pretty important for this.
- Functional wrappers can be written to suit an individual's way of doing things. However, I am interested to know if there is any fundamental or natural way of approaching this in Mathematica. There is clearly a natural way to do it in R.