Over a million developers have joined DZone.

How R2 Error Is Calculated in Generalized Linear Models

DZone's Guide to

How R2 Error Is Calculated in Generalized Linear Models

Learn how the R2 error is calculated for an H2O GLM (generalized linear model) — which awesomely uses the same math or any other statistical model!

· Big Data Zone ·
Free Resource

Access NoSQL and Big Data through SQL using standard drivers (ODBC, JDBC, ADO.NET). Free Download 

According to this article:

R-squared is a statistical measure of how close the data are to a fitted regression line. It is also known as the coefficient of determination, or the coefficient of multiple determination for multiple regression. [...] "100%" indicates that the model explains all the variability of the response data around its mean. 

You can get the full working Jupyter notebook for this article here directly from my GitHub.

This article explains how the R2 error is calculated for an H2O GLM (generalized linear model); note that the same math is used for any other statistical model. So, you can use this function anywhere you would want to apply it.

Let's build an H2O GLM model first:

import h2o
from h2o.estimators.glm import H2OGeneralizedLinearEstimator


local_url = "https://raw.githubusercontent.com/h2oai/sparkling-water/master/examples/smalldata/prostate.csv"
df = h2o.import_file(local_url)

feature_names = df.col_names

df_train, df_valid, df_test = df.split_frame(ratios=[0.8,0.1])

prostate_glm = H2OGeneralizedLinearEstimator(model_id = "prostate_glm")

prostate_glm.train(x = feature_names, y = y, training_frame=df_train, validation_frame=df_valid)

Calculate model performance based on training, validation, and test data:

train_performance = prostate_glm.model_performance(df_train)
valid_performance = prostate_glm.model_performance(df_valid)
test_performance = prostate_glm.model_performance(df_test)

Check the default R2 metrics for training, validation, and test data:


Get the prediction for the test data, which we kept separate:

predictions = prostate_glm.predict(df_test)

Here is the math used to calculate the R2 metric for the test dataset:

SSE = ((predictions-df_test[y])**2).sum()
y_hat = df_test[y].mean()
SST = ((df_test[y]-y_hat[0])**2).sum()

Get the model performance for the given test data as shown below:


Above we can see that both values — one given by model performance for test data and the other calculated by us — are same.

That's it; enjoy!

The fastest databases need the fastest drivers - learn how you can leverage CData Drivers for high performance NoSQL & Big Data Access.

h2o ,python ,glm ,big data ,r2 ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}