Learn R Programming

less (version 0.1.0)

LinearRegression: LinearRegression

Description

Wrapper R6 Class of stats::lm function that can be used for LESSRegressor and LESSClassifier

Arguments

Value

R6 Class of LinearRegression

Super classes

less::BaseEstimator -> less::SklearnEstimator -> LinearRegression

Methods

Inherited methods


Method fit()

Fits a linear model (y ~ X)

Usage

LinearRegression$fit(X, y)

Arguments

X

2D matrix or dataframe that includes predictors

y

1D vector or (n,1) dimensional matrix/dataframe that includes response variables

Returns

Fitted R6 Class of LinearRegression

Examples

data(abalone)
split_list <- train_test_split(abalone[1:100,], test_size =  0.3)
X_train <- split_list[[1]]
X_test <- split_list[[2]]
y_train <- split_list[[3]]
y_test <- split_list[[4]]

lr <- LinearRegression$new() lr$fit(X_train, y_train)


Method predict()

Predict regression value for X.

Usage

LinearRegression$predict(X0)

Arguments

X0

2D matrix or dataframe that includes predictors

Returns

The predict values.

Examples

lr <- LinearRegression$new()
lr$fit(X_train, y_train)
preds <- lr$predict(X_test)

lr <- LinearRegression$new() preds <- lr$fit(X_train, y_train)$predict(X_test)

preds <- LinearRegression$new()$fit(X_train, y_train)$predict(X_test) print(head(matrix(c(y_test, preds), ncol = 2, dimnames = (list(NULL, c("True", "Prediction"))))))


Method get_estimator_type()

Auxiliary function returning the estimator type e.g 'regressor', 'classifier'

Usage

LinearRegression$get_estimator_type()

Examples

lr$get_estimator_type()


Method clone()

The objects of this class are cloneable with this method.

Usage

LinearRegression$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

See Also

Examples

Run this code

## ------------------------------------------------
## Method `LinearRegression$fit`
## ------------------------------------------------

data(abalone)
split_list <- train_test_split(abalone[1:100,], test_size =  0.3)
X_train <- split_list[[1]]
X_test <- split_list[[2]]
y_train <- split_list[[3]]
y_test <- split_list[[4]]

lr <- LinearRegression$new()
lr$fit(X_train, y_train)

## ------------------------------------------------
## Method `LinearRegression$predict`
## ------------------------------------------------

lr <- LinearRegression$new()
lr$fit(X_train, y_train)
preds <- lr$predict(X_test)

lr <- LinearRegression$new()
preds <- lr$fit(X_train, y_train)$predict(X_test)

preds <- LinearRegression$new()$fit(X_train, y_train)$predict(X_test)
print(head(matrix(c(y_test, preds), ncol = 2, dimnames = (list(NULL, c("True", "Prediction"))))))

## ------------------------------------------------
## Method `LinearRegression$get_estimator_type`
## ------------------------------------------------

lr$get_estimator_type()

Run the code above in your browser using DataLab