Skip to main content

Linear regression

LINEAR_REGRESSION

linear_regression(Matrix s, Number learning_rate, Number max_iteration, Vector of Number ind_vars, Number predict_index, Vector of Number starting_weights) -> Vector
STABLE

Performs multi linear regression training over Matrix s.

  • s Matrix to use for training.
  • learning_rate learning rate for gradient descent algorithm
  • max_iterations max iterations for gradient descent algorithm
  • predict_index Index of value to predict using multi linear regression. Must be the index of a continous numeric value.
  • ind_vars Indices to use for predicting. Must be the indices of continous numeric values.
  • starting_weights Starting weights for the linear regression. Must be of size dim(ind_vars)+1. If omitted 0 is used at starting weights

Returns: a vector where index

  1. is the weight vector for the regression model
  2. is the sse of the model over the training data.

Example:

set :s = (select Vector of v
from Vector v
where v in csv:file_stream(system_models:folder("linear_regression")+
"test/linear_reg2.csv"));
set :v = linear_regression(:s,0.0000001,100,[1],2);
set :a = :v[1];
:v;
show_scatter_plot(350,350,{"float": "left"},{"no_stroke":1,"yrange":[0,4]});
select vector of y
from Vector of Number v, Vector of Number y
where v in :s
and y = [v[1], lr_estimate(:a,permute(v,[1])),2,2]
or v in :s
and y = concat(v,[2,1]);

linear_regression(Charstring model, Matrix dataset, Number learning_rate, Number max_iteration) -> Record
STABLE

Train linear regression model over dataset with the gived learning_rate

and max_iterations

  • model Name of model created using new_lr_model(Charstring,...) functions
  • dataset Matrix containing the dataset to train the model on.
  • learning_rate Larning rate for the gradient descent during training
  • max_iteration Maximum number of iterations to run gradient descent training.

Example:

new_lr_model("test",[1],2);

set :s = (select Vector of v
from Vector v
where v in csv:file_stream(system_models:folder("linear_regression")+
"test/linear_reg2.csv"));

set :model = linear_regression("test",:s,0.0000001,100);

show_markdown();
"#Linear regression for model "+:model["name"];
"The weights of the model are: `" + lr_weights(:model) + "`";
"The R-squared of the model is `" + :model["r2"] +"`";

show_scatter_plot(350,350,{"float": "left"},{"no_stroke":1,"yrange":[0,4]});
select vector of y
from Vector of Number v, Vector of Number y
where v in :s
and y = [v[1], lr_estimate("test", v),2,2]
or v in :s
and y = concat(v,[2,1]);


linear_regression(Matrix s, Number learning_rate, Number max_iteration, Vector of Number ind_vars, Number predict_index) -> Vector
STABLE

Performs linear regression training over finite Matrix s.


LR_ESTIMATE

lr_estimate(Vector of Number weights, Vector of Number input) -> Number
STABLE

Make an estimation using the weights from a trained model.


lr_estimate(Charstring model, Vector of Number input) -> Number
STABLE

Make an estimation on input with linear regression model named model


lr_estimate(Record model, Vector of Number input) -> Number
STABLE

Make an estimation on input with linear regression model


LR_IND_VARS

lr_ind_vars(Record lr_model) -> Vector of Number
STABLE

Retrieve the variable indices of the linear regression model record lr_model


LR_MODEL

lr_model(Charstring name) -> Record
STABLE

No description.


LR_PREDICT_INDEX

lr_predict_index(Record lr_model) -> Number
STABLE

Retrieve the prdiction index of the linear regression model record lr_model


LR_R2

lr_r2(Record model, Matrix dataset) -> Number
STABLE

Calculate the R-square for a linear regression model over the given dataset

-model Record containing model data, can be obtained with lr_model(Charstring)->Record -dataset Matrix containing the dataset to calculate R-squared on. Must be on the same format as the model was trained on.


lr_r2(Matrix dataset, Number predict_index, Vector of Number predictions) -> Number
STABLE

Calculates the R-squared metric given dataset, predict_index and

predictions

-dataset Matrix dataset to calculate R-squared for -predict_index What index of each vector in the dataset to calculate R-squared for. -predictions Vector of Number containing the predictions of the model over the dataset.


lr_r2(Charstring model, Matrix dataset) -> Number
STABLE

Convenience function to calculate R-squared for a linear regression model.

see lr_r2(Record model, Matrix dataset) -> Number for details.


LR_WEIGHTS

lr_weights(Record lr_model) -> Vector of Number
STABLE

Retrieve the wieghts of the linear regression model record lr_model


NEW_LR_MODEL

new_lr_model(Charstring name, Vector of Number ind_vars, Number predict_index, Vector of Number starting_weights) -> Object
STABLE

Creates a new linear regression model and stores the meta-data in

lr_model(Charstring name) -> Record -name Charstring name of the model -ind_vars Indices of the independent variable to use in the training set. Must be the indices of continous numeric values -predict_index Index of the varaible to predict from the training set. Must be the index of a continous numeric value. -starting_weights (Optional) Vector of number containing the starting weights of the model. must be of size dim(ind_vars)+1 where the first value is the intercept weight.


new_lr_model(Charstring name, Vector of Number ind_vars, Number predict_index) -> Object
STABLE

No description.