Published
Edited
Oct 25, 2021
Importers
Insert cell
# Linear and Logistic Regression

To import:
```javascript
import {logistic_regression, linear_regression} from "@herbps10/regression"
```
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
bernoulli_log_likelihood = (data, x, y, beta) => {
let ll = 0;
for(let i = 0; i < data.length; i++) {
ll += dbernoulli_log(data[i][y], inv_logit(dot(beta, data[i][x])));
}
return ll;
}
Insert cell
bernoulli_log_likelihood_prime = (data, x, y, beta) => {
const derivatives = new Array(beta.length).fill(0);
for(let j = 0; j < beta.length; j++) {
for(let i = 0; i < data.length; i++) {
derivatives[j] += (data[i][y] - inv_logit(dot(beta, data[i][x]))) * data[i][x][j];
}
}
return derivatives;
}
Insert cell
squared_error_loss = (data, x, y, beta) => {
let loss = 0;
for(let i = 0; i < data.length; i++) {
loss += -Math.pow(data[i][y] - dot(beta, data[i][x]), 2);
}
return loss;
}
Insert cell
squared_error_loss_prime = (data, x, y, beta) => {
const derivatives = new Array(beta.length).fill(0);
for(let j = 0; j < beta.length; j++) {
for(let i = 0; i < data.length; i++) {
derivatives[j] += 2 * (data[i][y] - dot(beta, data[i][x])) * data[i][x][j];
}
}
return derivatives;
}
Insert cell
Insert cell
logistic_regression = (data, x, y, options) => {
options = Object.assign({
learning_rate: 1 / data.length,
beta0: new Array(data[0][x].length).fill(1),
maxiter: 1000
}, options);

const results = gradient_ascent((beta) => bernoulli_log_likelihood(data, x, y, beta), (beta) => bernoulli_log_likelihood_prime(data, x, y, beta), options.beta0, options.maxiter, options.learning_rate);


const predict = (x) => {
return inv_logit(dot(x, results.x));
}
const predictions = data.map((d) => {
return predict(d[x]);
});
return ({
beta: results.x,
predictions: predictions,
gradient_ascent: results,
predict: predict
});
return results;
}
Insert cell
linear_regression = (data, x, y, options) => {
options = Object.assign({
learning_rate: 1 / data.length,
beta0: new Array(data[0][x].length).fill(1),
maxiter: 1000
}, options);

const results = gradient_ascent((beta) => squared_error_loss(data, x, y, beta), (beta) => squared_error_loss_prime(data, x, y, beta), options.beta0, options.maxiter, options.learning_rate);
const predict = (x) => {
return dot(x, results.x);
};
const predictions = data.map((d) => {
return predict(d[x]);
});

return ({
beta: results.x,
predictions: predictions,
gradient_ascent: results,
predict: predict
});
return results;
}
Insert cell
logistic_regression(data, "x", "y")
Insert cell
linear_regression(data, "x", "y")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more