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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more