Formulas provide a declarative way to specify statistical models, inspired by R.
response ~ predictor
The ~ operator creates a Formula object that can be
passed to modeling functions.
-- Simple linear regression
model = lm(data = df, formula = y ~ x)
-- Future: Multiple regression
model = lm(data = df, formula = y ~ x1 + x2 + x3)
lm() - Linear regressionlm() - Linear
RegressionFit a linear model using least squares.
lm(data: DataFrame, formula: Formula, ...) -> Dict
data: DataFrame containing the variablesformula: Formula specifying the model (e.g.,
y ~ x)Dictionary containing: - formula: The model formula -
intercept: Estimated intercept - slope:
Estimated slope (coefficient) - r_squared: R² statistic -
residuals: Vector of residuals - n: Number of
observations - response: Name of response variable -
predictor: Name of predictor variable
data = read_csv("mtcars.csv")
model = lm(data = data, formula = mpg ~ hp)
print(model.r_squared)
y ~ x + 1 vs
y ~ x - 1y ~ x1 * x2y ~ log(x)