{"id":39,"date":"2014-10-22T04:18:04","date_gmt":"2014-10-22T04:18:04","guid":{"rendered":"http:\/\/gallego.ca\/blog\/?p=39"},"modified":"2022-04-23T19:54:12","modified_gmt":"2022-04-23T19:54:12","slug":"linear-models-for-classification","status":"publish","type":"post","link":"http:\/\/gallego.ca\/myblog\/linear-models-for-classification\/","title":{"rendered":"Linear Models for classification"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are two different approaches to determining the conditional probabilities \\(p(C_k|x)\\). One technique is to model them directly, for example by representing them as parametric models and then optimizing the parameters using a training set. Alternatively, we can adopt a Bayesian approach in which we model the class-conditional densities given by \\(p(x|C_k)\\), together with the prior probabilities \\(p(C_k)\\) for the classes, and then we compute the required posterior probabilities using Bayes theorem \\(p(C_k|x)= p(x|C_k)p(C_k)\\)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the simplest case, a model is linear in the input variables and therefore takes the form \\(y(x)= w^Tx+w_0\\), so that \\(y\\) is a real number. For classification problems, however, we wish to predict discrete class labels, or more generally posterior probabilities that lie in the range (0, 1). we transform the linear function of w using a nonlinear function f( \u00b7 ) so that \\(y(x)=f(w^Tx+w_0)\\)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\\(f( \u00b7 )\\) is known as an activation function. hence the decision surfaces are linear functions of x, even if the function f(\u00b7) is nonlinear. For this reason, these models are called generalized linear models.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basis Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest linear model for regression is one that involves a linear combination of the input variables<br>\\(y(x,w)= w_0 +w_1x_1 +&#8230;+w_Dx_D\\),<br>The key property of this model is that it is a linear function of the parameters \\(w_0,&#8230;,w_D\\). It is also a linear function of the input variables \\(xi\\).<br>We can extend this class of models by considering linear combinations of fixed nonlinear functions of the input variables, of the form \\(\u03c6j(x)\\) are known as basis functions.<br>if:<br>$$\\phi =(\\phi_0,&#8230;,\\phi_{M\u22121})$$ and<br>$$w =(w_0,&#8230;,w_{M\u22121})$$<br>we can write the simplest linear classifier in matrix form as:<br>$$y(x)= w^Tx+w_0$$<br>The parameter \\(w_0\\) allows for any fixed offset in the data and is sometimes called a bias parameter or threshold.<br>An input vector x is assigned to class \\(C1\\) if \\(y(x) = 0\\) and to class \\(C2\\) otherwise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fisher\u2019s linear discriminant<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Considering a two-class problem in which there are $N1$ points of class \\(C1\\) and \\(N2\\) points of class \\(C2\\), so that the mean vectors of the two classes are \\(m1 = average(N1)\\) and \\(m2 = average(N2)\\)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest measure of the separation of the classes, when projected onto \\(w\\), is the separation of the projected class means. This suggests that we might choose \\(w\\) so as to maximize<br>$$m2 \u2212 m1 = w^T(m2 \u2212m1)$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s a problem with this approach, two classes that are well separated in the original two-dimensional space \\((x1,x2)\\) but can have considerable overlap when projected onto the line joining their means. This difficulty arises from the strongly nondiagonal covariances of the class distributions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The idea proposed by Fisher is to maximize a function that will give a large separation between the projected class means while also giving a small variance within each class, thereby minimizing the class overlap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Fisher criterion is defined to be the ratio of the between-class variance to the within-class variance and is given by<br>$$J(w) = (m2 \u2212 m1)^2\/(s1^2 + s2^2)$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The least-squares approach to the determination of a linear discriminant was based on the goal of making the model predictions as close as possible to a set of target values. By contrast, the Fisher criterion was derived by requiring maximum class separation in the output space.<\/p>\n\n\n\n<div>\n<h2>Example: Anderson&#8217;s Iris Data<\/h2>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Dataset: (Fisher&#8217;s or Anderson&#8217;s) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.<br><a href=\"http:\/\/commons.wikimedia.org\/wiki\/File:Kosaciec_szczecinkowaty_Iris_setosa.jpg#mediaviewer\/File:Kosaciec_szczecinkowaty_Iris_setosa.jpg\">Iris setosa<\/a><br><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_setosa.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-43\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_setosa-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_setosa-300x225.jpg 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_setosa-1024x768.jpg 1024w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_setosa.jpg 1280w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br><a href=\"http:\/\/commons.wikimedia.org\/wiki\/File:Iris_versicolor_3.jpg#mediaviewer\/File:Iris_versicolor_3.jpg\">Iris versicolor:<\/a><br><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_versicolor.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-44\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_versicolor-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_versicolor-300x225.jpg 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_versicolor-1024x768.jpg 1024w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_versicolor.jpg 1984w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br><a href=\"http:\/\/commons.wikimedia.org\/wiki\/File:Iris_virginica.jpg#mediaviewer\/File:Iris_virginica.jpg\">Iris virginica:<\/a><br><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_virginica.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-45\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_virginica-300x244.jpg\" alt=\"\" width=\"300\" height=\"244\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_virginica-300x244.jpg 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/Iris_virginica-1024x833.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br>Licensed under <a title=\"Creative Commons Attribution-Share Alike 2.0\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/2.0\">CC BY-SA 2.0<\/a> via <a href=\"\/\/commons.wikimedia.org\/wiki\/\">Wikimedia Commons<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Toy data: Iris binary classification, 4 features, 2 classes, n1=50\/100, n2=50\/100\n# train: sample 0.75 of data\n# C1 = iris setosa, C2 = iris versicolor\nirisbin = subset(iris, Species==\"setosa\" | Species==\"versicolor\")\nset.seed(14102014)\ntrain &lt;- sample(1:100, 75)\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"># \"Anderson's Iris Data -- 2 species well separated classes\"\nirisdata = data.frame( rbind(iris3[,,1], iris3[,,2]), Species = rep(c(\"setosa\",\"versicolor\"), rep(50,2)))\npairs(irisdata[1:4], main = \"Anderson's Iris Data -- 2 species well separated classes\",\n      pch = 21, bg = c(\"red\", \"blue\")[unclass(irisdata$Species)])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-1-300x300.png\" alt=\"\" class=\"wp-image-48\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-1-300x300.png 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-1-150x150.png 150w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-1.png 504w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">table(irisdata$Species[train])\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## \n##     setosa versicolor \n##         40         35\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">ldairis &lt;- lda(Species ~ ., irisdata, subset = train)\nprint(ldairis)\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"># Call:\n## lda(Species ~ ., data = irisdata, subset = train)\n## \n## Prior probabilities of groups:\n##     setosa versicolor \n##  0.5333333  0.4666667 \n## \n## Group means:\n##            Sepal.L. Sepal.W. Petal.L. Petal.W.\n## setosa     4.990000   3.4025 1.480000 0.245000\n## versicolor 5.922857   2.7600 4.268571 1.328571\n## \n## Coefficients of linear discriminants:\n##                 LD1\n## Sepal.L. -0.3704904\n## Sepal.W. -1.5516582\n## Petal.L.  2.1819881\n## Petal.W.  2.5278116\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">partimat(Species ~ ., data=irisdata, method=\"lda\", subset=train, na.action=\"na.omit\", plot.matrix=TRUE, print.err = 0.9)\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-2-300x300.png\" alt=\"\" class=\"wp-image-49\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-2-300x300.png 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-2-150x150.png 150w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-2.png 504w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Performance on train data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">confusionMatrix(predict(ldairis, irisdata[train,])$class, as.factor(irisdata[train,5]) )\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Confusion Matrix and Statistics\n## \n##             Reference\n## Prediction   setosa versicolor\n##   setosa         40          0\n##   versicolor      0         35\n##                                     \n##                Accuracy : 1         \n##                  95% CI : (0.952, 1)\n##     No Information Rate : 0.5333    \n##     P-Value [Acc &gt; NIR] : &lt; 2.2e-16 \n##                                     \n##                   Kappa : 1         \n##  Mcnemar's Test P-Value : NA        \n##                                     \n##             Sensitivity : 1.0000    \n##             Specificity : 1.0000    \n##          Pos Pred Value : 1.0000    \n##          Neg Pred Value : 1.0000    \n##              Prevalence : 0.5333    \n##          Detection Rate : 0.5333    \n##    Detection Prevalence : 0.5333    \n##       Balanced Accuracy : 1.0000    \n##                                     \n##        'Positive' Class : setosa    \n##\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Performance on test data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">confusionMatrix(predict(ldairis, irisdata[-train,])$class, as.factor(irisdata[-train,5]) )<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Confusion Matrix and Statistics\n## \n##             Reference\n## Prediction   setosa versicolor\n##   setosa         10          0\n##   versicolor      0         15\n##                                      \n##                Accuracy : 1          \n##                  95% CI : (0.8628, 1)\n##     No Information Rate : 0.6        \n##     P-Value [Acc &gt; NIR] : 2.843e-06  \n##                                      \n##                   Kappa : 1          \n##  Mcnemar's Test P-Value : NA         \n##                                      \n##             Sensitivity : 1.0        \n##             Specificity : 1.0        \n##          Pos Pred Value : 1.0        \n##          Neg Pred Value : 1.0        \n##              Prevalence : 0.4        \n##          Detection Rate : 0.4        \n##    Detection Prevalence : 0.4        \n##       Balanced Accuracy : 1.0        \n##                                      \n##        'Positive' Class : setosa     \n## \n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since setosa and versicolor are so well separated in feature space, our linear discriminants have to problem is perfectly separating the classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now if we look at versicolor and virginica, they produce a different feature set distribution:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \"Anderson's Iris Data -- 2 species some overlaps in classes\"\nirisdata = data.frame( rbind(iris3[,,2], iris3[,,3]), Species = rep(c(\"versicolor\",\"virginica\"), rep(50,2)))\npairs(irisdata[1:4], main = \"Anderson's Iris Data -- 2 species some overlaps in classes\",\n      pch = 21, bg = c(\"red\", \"green3\")[unclass(irisdata$Species)])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-3-300x300.png\" alt=\"\" class=\"wp-image-50\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-3-300x300.png 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-3-150x150.png 150w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-3.png 504w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">table(irisdata$Species[train])\n## \n## versicolor  virginica \n##         40         35\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">ldairis &lt;- lda(Species ~ ., irisdata, subset = train)\nprint(ldairis)\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Call:\n## lda(Species ~ ., data = irisdata, subset = train)\n## \n## Prior probabilities of groups:\n## versicolor  virginica \n##  0.5333333  0.4666667 \n## \n## Group means:\n##            Sepal.L. Sepal.W. Petal.L. Petal.W.\n## versicolor   5.8925 2.760000 4.250000 1.330000\n## virginica    6.6800 3.008571 5.611429 2.071429\n## \n## Coefficients of linear discriminants:\n##                 LD1\n## Sepal.L. -0.3898487\n## Sepal.W. -1.5466266\n## Petal.L.  1.2755075\n## Petal.W.  3.5679695\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">partimat(Species ~ ., data=irisdata, method=\"lda\", subset=train, na.action=\"na.omit\", plot.matrix=TRUE, print.err = 0.9)\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-4-300x300.png\" alt=\"\" class=\"wp-image-51\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-4-300x300.png 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-4-150x150.png 150w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-2-4.png 504w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Performance on train data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">confusionMatrix(predict(ldairis, irisdata[train,])$class, as.factor(irisdata[train,5]) )\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Confusion Matrix and Statistics\n## \n##             Reference\n## Prediction   versicolor virginica\n##   versicolor         39         1\n##   virginica           1        34\n##                                          \n##                Accuracy : 0.9733         \n##                  95% CI : (0.907, 0.9968)\n##     No Information Rate : 0.5333         \n##     P-Value [Acc &gt; NIR] : &lt;2e-16         \n##                                          \n##                   Kappa : 0.9464         \n##  Mcnemar's Test P-Value : 1              \n##                                          \n##             Sensitivity : 0.9750         \n##             Specificity : 0.9714         \n##          Pos Pred Value : 0.9750         \n##          Neg Pred Value : 0.9714         \n##              Prevalence : 0.5333         \n##          Detection Rate : 0.5200         \n##    Detection Prevalence : 0.5333         \n##       Balanced Accuracy : 0.9732         \n##                                          \n##        'Positive' Class : versicolor     \n## \n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Performance on test data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">confusionMatrix(predict(ldairis, irisdata[-train,])$class, as.factor(irisdata[-train,5]) )\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Confusion Matrix and Statistics\n## \n##             Reference\n## Prediction   versicolor virginica\n##   versicolor         10         0\n##   virginica           0        15\n##                                      \n##                Accuracy : 1          \n##                  95% CI : (0.8628, 1)\n##     No Information Rate : 0.6        \n##     P-Value [Acc &gt; NIR] : 2.843e-06  \n##                                      \n##                   Kappa : 1          \n##  Mcnemar's Test P-Value : NA         \n##                                      \n##             Sensitivity : 1.0        \n##             Specificity : 1.0        \n##          Pos Pred Value : 1.0        \n##          Neg Pred Value : 1.0        \n##              Prevalence : 0.4        \n##          Detection Rate : 0.4        \n##    Detection Prevalence : 0.4        \n##       Balanced Accuracy : 1.0        \n##                                      \n##        'Positive' Class : versicolor \n## \n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Despite the feature distribution overlap, the LDA does a great job and produces very low error rates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Quadratic Discriminant Analysis (QDA)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike LDA, QDA Estimates the covariance matrix separately for each class \\(k\\).<br>Decision boundaries are quadratic equations in \\(x\\). QDA fits the data better than LDA, but has more parameters to estimate.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \"Anderson's Iris Data -- 2 species some overlaps in classes\"\nqdairis &lt;- qda(Species ~ ., irisdata, subset = train)\nprint(qdairis)\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Call:\n## qda(Species ~ ., data = irisdata, subset = train)\n## \n## Prior probabilities of groups:\n## versicolor  virginica \n##  0.5333333  0.4666667 \n## \n## Group means:\n##            Sepal.L. Sepal.W. Petal.L. Petal.W.\n## versicolor   5.8925 2.760000 4.250000 1.330000\n## virginica    6.6800 3.008571 5.611429 2.071429\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">partimat(Species ~ ., data=irisdata, method=\"qda\", subset=train, na.action=\"na.omit\", plot.matrix=TRUE, print.err = 0.9)\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-3-11.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-3-11-300x300.png\" alt=\"\" class=\"wp-image-52\" srcset=\"http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-3-11-300x300.png 300w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-3-11-150x150.png 150w, http:\/\/gallego.ca\/myblog\/wp-content\/uploads\/sites\/2\/2014\/11\/unnamed-chunk-3-11.png 504w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"># performance on train data\nconfusionMatrix(predict(qdairis, irisdata[train,])$class, as.factor(irisdata[train,5]) )\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Confusion Matrix and Statistics\n## \n##             Reference\n## Prediction   versicolor virginica\n##   versicolor         38         1\n##   virginica           2        34\n##                                           \n##                Accuracy : 0.96            \n##                  95% CI : (0.8875, 0.9917)\n##     No Information Rate : 0.5333          \n##     P-Value [Acc &gt; NIR] : &lt;2e-16          \n##                                           \n##                   Kappa : 0.9198          \n##  Mcnemar's Test P-Value : 1               \n##                                           \n##             Sensitivity : 0.9500          \n##             Specificity : 0.9714          \n##          Pos Pred Value : 0.9744          \n##          Neg Pred Value : 0.9444          \n##              Prevalence : 0.5333          \n##          Detection Rate : 0.5067          \n##    Detection Prevalence : 0.5200          \n##       Balanced Accuracy : 0.9607          \n##                                           \n##        'Positive' Class : versicolor      \n## \n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"># performance on test data\nconfusionMatrix(predict(qdairis, irisdata[-train,])$class, as.factor(irisdata[-train,5]) )\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">## Confusion Matrix and Statistics\n## \n##             Reference\n## Prediction   versicolor virginica\n##   versicolor         10         0\n##   virginica           0        15\n##                                      \n##                Accuracy : 1          \n##                  95% CI : (0.8628, 1)\n##     No Information Rate : 0.6        \n##     P-Value [Acc &gt; NIR] : 2.843e-06  \n##                                      \n##                   Kappa : 1          \n##  Mcnemar's Test P-Value : NA         \n##                                      \n##             Sensitivity : 1.0        \n##             Specificity : 1.0        \n##          Pos Pred Value : 1.0        \n##          Neg Pred Value : 1.0        \n##              Prevalence : 0.4        \n##          Detection Rate : 0.4        \n##    Detection Prevalence : 0.4        \n##       Balanced Accuracy : 1.0        \n##                                      \n##        'Positive' Class : versicolor \n##\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this dataset, increasing the complexity of the classifier did not represent an improvement of the error rates (false positives and false negatives). Therefore, since you should always strive to keep the model with the lowest complexity that performs the best, we can propose that an LDA does just fine in classifying versicolor and virginica flowers using the petal and sepal length and petal.<br>For a Bayesian interpretation of discriminant analysis read another post <a title=\"Discriminant analysis from a Bayesian point of view\" href=\"http:\/\/gallego.ca\/blog\/discriminant-analysis-from-a-bayesian-point-of-view\/\" target=\"_blank\" rel=\"noopener\">here<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">(<a href=\"http:\/\/research.microsoft.com\/en-us\/um\/people\/cmbishop\/PRML\/\" target=\"_blank\" rel=\"noopener\">Bishop<\/a> and <a href=\"http:\/\/statweb.stanford.edu\/~tibs\/ElemStatLearn\/\" target=\"_blank\" rel=\"noopener\">Hastie<\/a> textbooks)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two different approaches to determining the conditional probabilities . One technique is to model them directly, for example by representing them as parametric models and then optimizing the parameters using a training set. Alternatively, we can adopt a Bayesian approach in which we model the class-conditional densities given by , together with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":51,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-machine-learning"],"_links":{"self":[{"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":13,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":133,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/posts\/39\/revisions\/133"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/media\/51"}],"wp:attachment":[{"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/gallego.ca\/myblog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}