Generalized linear regression
Contrasted with linear regression where the output is assumed to follow a Gaussian distribution, generalized linear models (GLMs) are specifications of linear models where the response variable Yi follows some distribution from the exponential family of distributions. Spark’s GeneralizedLinearRegression interface allows for flexible specification of GLMs which can be used for various types of prediction problems including linear regression, Poisson regression, logistic regression, and others. Currently in spark.ml, only a subset of the exponential family distributions are supported and they are listed below.
NOTE: Spark currently only supports up to 4096 features through its GeneralizedLinearRegression interface, and will throw an exception if this constraint is exceeded.
Spark’s generalized linear regression interface also provides summary statistics for diagnosing the fit of GLM models, including residuals, p-values, deviances, the Akaike information criterion, and others.
1
import org.apache.spark.ml.regression.GeneralizedLinearRegression
2
​
3
// Load training data
4
val dataset = spark.read.format("libsvm")
5
.load("data/mllib/sample_linear_regression_data.txt")
6
​
7
val glr = new GeneralizedLinearRegression()
8
.setFamily("gaussian")
9
.setLink("identity")
10
.setMaxIter(10)
11
.setRegParam(0.3)
12
​
13
// Fit the model
14
val model = glr.fit(dataset)
15
​
16
// Print the coefficients and intercept for generalized linear regression model
17
println(s"Coefficients: ${model.coefficients}")
18
println(s"Intercept: ${model.intercept}")
19
​
20
// Summarize the model over the training set and print out some metrics
21
val summary = model.summary
22
println(s"Coefficient Standard Errors: ${summary.coefficientStandardErrors.mkString(",")}")
23
println(s"T Values: ${summary.tValues.mkString(",")}")
24
println(s"P Values: ${summary.pValues.mkString(",")}")
25
println(s"Dispersion: ${summary.dispersion}")
26
println(s"Null Deviance: ${summary.nullDeviance}")
27
println(s"Residual Degree Of Freedom Null: ${summary.residualDegreeOfFreedomNull}")
28
println(s"Deviance: ${summary.deviance}")
29
println(s"Residual Degree Of Freedom: ${summary.residualDegreeOfFreedom}")
30
println(s"AIC: ${summary.aic}")
31
println("Deviance Residuals: ")
32
summary.residuals().show()
33
​
34
/*
35
Output:
36
Coefficients: [0.010541828081257216,0.8003253100560949,-0.7845165541420371,2.3679887171421914,0.5010002089857577,1.1222351159753026,-0.2926824398623296,-0.49837174323213035,-0.6035797180675657,0.6725550067187461]
37
Intercept: 0.14592176145232041
38
Coefficient Standard Errors: 0.7950428434287478,0.8049713176546897,0.7975916824772489,0.8312649247659919,0.7945436200517938,0.8118992572197593,0.7919506385542777,0.7973378214726764,0.8300714999626418,0.7771333489686802,0.463930109648428
39
T Values: 0.013259446542269243,0.9942283563442594,-0.9836067393599172,2.848657084633759,0.6305509179635714,1.382234441029355,-0.3695715687490668,-0.6250446546128238,-0.7271418403049983,0.8654306337661122,0.31453393176593286
40
P Values: 0.989426199114056,0.32060241580811044,0.3257943227369877,0.004575078538306521,0.5286281628105467,0.16752945248679119,0.7118614002322872,0.5322327097421431,0.467486325282384,0.3872259825794293,0.753249430501097
41
Dispersion: 105.60988356821714
42
Null Deviance: 53229.3654338832
43
Residual Degree Of Freedom Null: 500
44
Deviance: 51748.8429484264
45
Residual Degree Of Freedom: 490
46
AIC: 3769.1895871765314
47
Deviance Residuals:
48
+-------------------+
49
| devianceResiduals|
50
+-------------------+
51
|-10.974359174246889|
52
| 0.8872320138420559|
53
| -4.596541837478908|
54
|-20.411667435019638|
55
|-10.270419345342642|
56
|-6.0156058956799905|
57
|-10.663939415849267|
58
| 2.1153960525024713|
59
| 3.9807132379137675|
60
|-17.225218272069533|
61
| -4.611647633532147|
62
| 6.4176669407698546|
63
| 11.407137945300537|
64
| -20.70176540467664|
65
| -2.683748540510967|
66
|-16.755494794232536|
67
| 8.154668342638725|
68
|-1.4355057987358848|
69
|-0.6435058688185704|
70
| -1.13802589316832|
71
+-------------------+
72
only showing top 20 rows
73
​
74
​
75
*/
Copied!
Last modified 1yr ago
Copy link