{"id":251,"date":"2017-12-06T15:50:49","date_gmt":"2017-12-06T13:50:49","guid":{"rendered":"http:\/\/benediktehinger.de\/blog\/science\/?p=251"},"modified":"2017-12-06T17:33:41","modified_gmt":"2017-12-06T15:33:41","slug":"__trashed","status":"publish","type":"post","link":"https:\/\/benediktehinger.de\/blog\/science\/__trashed\/","title":{"rendered":"Stretching the axes; visualizing non-linear linear regression"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>From time to time I explain concepts and ideas to my students.<\/p>\n<h2>Background<\/h2>\n<p>Often this pops up in a statistical context, when one has a non-linear dependency between the to-be-predicted variable and the predictor-variables. By transforming the predictors, relationships can be made linear, i.e. a logarithmic (exponential, quadratic etc.) relationships can be modeled by a **linear** model.<\/p>\n<h2>The idea<\/h2>\n<p>I have a very visual understanding on basis-functions \/ non-linear transformation of variables in terms of stretching \/ condensing the basis (the x-axis here). This can also be applied to the generalized linear model (here for logistic regression).<\/p>\n<p>Imagine that the x-axis of a plot is made of some kind of elastic material, you can stretch and condense it. Of course you do not need to stretch every part equally, one example would be to stretch parts that are far away from zero, exponentially more than parts that are close to zero. If you would have an exponential relationship ($ y = e^x$) then $y$ would now lie on a straight line.<\/p>\n<h2>TLDR;<\/h2>\n<p>Imagine you have a non-linear relationship, by stretching the x-axis in accordance to that non-linear relationship, you will have a linear relationship.<\/p>\n<h2>An exemplary non-linear relationship:<\/h2>\n<p><a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-3-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-258 size-medium\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-3-1-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-3-1-300x300.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-3-1-145x145.png 145w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-3-1-768x768.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-3-1.png 960w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>We want to do $y = b_0 + b_1x $ but obviously a linear line does not fit well. We can do something called polynomial expansion, i.e. add more predictors which are simple transformations of the predictor $x$. i.e. $y = b_0 + b_1x + b_2x^2 + b_3x^3$<br \/>\n<a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-4-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-252 size-medium\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-4-1-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-4-1-300x300.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-4-1-145x145.png 145w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-4-1-768x768.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-4-1.png 960w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The trick comes here: We can interpret the new $x^3$ basis function as a stretching of the x-axis. I.e. the further we move out on the x-axis, the longer we need to stretch the parts (exactly by x^3 times)<br \/>\n<a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-5-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-253\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-5-1.png\" alt=\"\" width=\"1920\" height=\"768\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-5-1.png 1920w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-5-1-300x120.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-5-1-768x307.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-5-1-1024x410.png 1024w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>This can be shown also for other functions:<\/p>\n<h3>Exponential<\/h3>\n<p><a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-6-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-254\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-6-1.png\" alt=\"\" width=\"1920\" height=\"768\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-6-1.png 1920w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-6-1-300x120.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-6-1-768x307.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-6-1-1024x410.png 1024w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h3>Logarithmic<\/h3>\n<p>Note that the logarithm is not defined for negative numbers<br \/>\n<a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-7-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-255\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-7-1.png\" alt=\"\" width=\"1920\" height=\"768\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-7-1.png 1920w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-7-1-300x120.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-7-1-768x307.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-7-1-1024x410.png 1024w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h3>Quadratic<\/h3>\n<p>Note how the stretching can be negative, i.e. the original negative values are stretching\/transformed to positive values<br \/>\n<a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-8-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-256\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-8-1.png\" alt=\"\" width=\"1920\" height=\"768\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-8-1.png 1920w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-8-1-300x120.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-8-1-768x307.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-8-1-1024x410.png 1024w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h2>Using the trick on the y-axis<\/h2>\n<p>One can interprete **logistic regression** with the same trick:<br \/>\n$$ g{-1}(y) = b_0 + b_1*x &lt;=&gt; y = g(b_0+b_1x)$$<br \/>\nwith $g$, the logistic (logit) function and $g^{-1}$ the inverse logistic function (invlogit)<br \/>\n$$ g^{-1} = \\ln\\frac{p}{1-p} &lt;=&gt; g = \\frac{1}{1+e^{-x}}$$<\/p>\n<p>Usually we would have some non-linear dependency on a probabilty of e.g. success. That means, with a low value of x, your success-chance are low. To model this kind of data, one can transform the y-axis using $g$ above.<\/p>\n<p><a href=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-9-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-257\" src=\"http:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-9-1.png\" alt=\"\" width=\"1920\" height=\"1344\" srcset=\"https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-9-1.png 1920w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-9-1-300x210.png 300w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-9-1-768x538.png 768w, https:\/\/benediktehinger.de\/blog\/science\/upload\/sites\/2\/2017\/11\/unnamed-chunk-9-1-1024x717.png 1024w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; From time to time I explain concepts and ideas to my students. Background Often this pops up in a statistical context, when one has a non-linear dependency between the to-be-predicted variable and the predictor-variables. By transforming the predictors, relationships can be made linear, i.e. a logarithmic (exponential, quadratic etc.) relationships can be modeled by a **linear** model. The idea I have a very visual understanding on basis-functions \/ non-linear transformation of variables in terms of stretching \/ condensing the basis (the x-axis here). This can also be applied to the generalized linear model (here for logistic regression). Imagine that&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-251","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":0,"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"wp:attachment":[{"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benediktehinger.de\/blog\/science\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}