Visualizing Multiple Regression in 3D

栏目: IT技术 · 发布时间: 4年前

内容简介:No matter your exposure to data science & the world of statistics, at the very least, you’ve very likely heard of regression. In this post we’ll be talking about multiple regression, as a precursor, you’ll definitely want some familiarity with simple linea

How Can You Make More Sense of High Dimensional Datasets?

Jul 27 ·6min read

Visualizing Multiple Regression in 3D

Image by Mediamodifier from Pixabay

Introduction

No matter your exposure to data science & the world of statistics, at the very least, you’ve very likely heard of regression. In this post we’ll be talking about multiple regression, as a precursor, you’ll definitely want some familiarity with simple linear regression. If you aren’t familiar you canstart here! Otherwise, let’s dive in with multiple linear regression. I recently spoke about visualizing multiple linear regression with heatmaps, if you’ve already read that post, feel free to jump down to the modeling section of this post where we’ll build our new model and introduce the plotly package and 3 dimensional visualizaiton. If you haven't read it, this is another helpful way to visualizemultiple regression.

Multiple Linear Regression

The distinction we draw between simple linear regression and multiple linear regression is simply the number of explanatory variables that help us understand our dependent variable.

Multiple linear regression is an incredibly popular statistical technique for data scientists and is foundational to a lot of the more complex methodologies used by data scientists.

In mypost on simple linear regression, I gave the example of predicting home prices using a single numeric variable — square footage.

This post is a part of a series of posts where we explore different implementations of linear regression. In a post where we explore the parallel slopes model , we create a model where we predict price using square footage and whether it’s a waterfront property or not. Here we’ll do something similar, but we’ll create our model using multiple numeric inputs.

Let’s Get Modeling

Similar to what we’ve built in the aforementioned posts, we’ll create a linear regression model where we add a new numeric variable.

The dataset we’re working with is a Seattle home prices dataset. The record level of the dataset is by home and details price, square footage, # of beds, # of baths, and so forth.

Through the course of this post, we’ll be trying to explain price through a function of other numeric variables in the dataset.

With that said, let’s dive in. Similar to what we’ve built previously we’re using sqft_living to predict price , only here we'll add another variable: sqft_basement

fit <- lm(price ~  sqft_living + sqft_basement,    
          data = housing)
summary(fit)

Visualizing Multiple Regression in 3D

The inclusion of various numeric explanatory variables to a regression model is simple syntactically as well as mathematically.

Visualization Limitations

While you can technically layer numeric variables one after another into the same model, it can quickly become difficult to visualize and understand.

In the case of our model, we have three separate dimensions we’ll need to be able to assess.

As I mentioned previously, here we will be using plotly 's 3d plotting tools to generate deeper understanding.

Let’s play around with plot_ly !

Let’s first visualize sqft_living and price to familiarize ourselves with the syntax.

plot_ly(data = housing, x = ~sqft_living, y = ~price, opacity = 0.5) %>%
  add_markers()

Visualizing Multiple Regression in 3D

As you can see the syntax isn’t too different from ggplot. First specify the data, then jump into the aesthetics without having to explicitly declare that they’re aesthetics. The above visual is a simple 2 dimensional scatter plot.

Let’s visualize in 3 dimensions!

plot_ly(data = housing, z = ~price, x = ~sqft_living, y = ~bathrooms, opacity = 0.5) %>%
  add_markers()

Visualizing Multiple Regression in 3D

Similar to what we did before, we’ve just moved price to the z-axis and now included sqft_basement . What's fun about this plotting tool is that it's not static, you can click and drag rotating the angle from which you're viewing the plot. Obviously here you're just seeing a screenshot, but get this running on your own machine to experience the full flexibility of plotly . At the moment you run this command in RStudio, your Viewer window will populate with this dragable/moveable visual that lends well to interpreting a dataset of greater dimensions.

Adding a Plane

When moving from two dimensions to three dimensions, things change. If you have background in linear algebra this may resonate. To put it simply, if you have a single dimension, then you have a point. If you have two dimensions you have a line. If you have three dimensions… you have a plane .

Having this in mind, Let’s visualize our multiple linear regression model with a plane.

First things first we need to create a matrix with all possible model inputs as well as the model prediction in each case.

Below I create a vector for our x and our y . We then pass them to the outer function where we declare the operation of passing them both to the linear regression function defined through the fitting of our model.

x <- seq(370, 15000, by = 10)
y <- seq(0, 15000, by = 10)plane <- outer(x, y, function(a, b){fit$coef[1] + 
    fit$coef[2]*a + fit$coef[3]*b})

Now that we have our plane, let’s add it to our visual.

plot_ly(data = housing, z = ~price, x = ~sqft_living, y = ~sqft_basement, opacity = 0.5) %>%
  add_markers() %>%
  add_surface(x = ~x, y = ~y, z = ~plane, showscale = FALSE)

Visualizing Multiple Regression in 3D

Again, you’ve got to jump in and play with plotly yourself.

You’ve done it! You’ve added a plane to your 3D scatter plot that represents the relationship between our regression formula and different inputs of sqft_lot & sqft_basement , but we still have a question… how does this help us?

Have you ever added a regression line to your 2D scatter plot? If so, what was the intention?

You would add a line to your plot to give an indication of what the ‘best fit’ looks like, but it’s also useful to be able to say for a given value of x , we would predict y . The plane gives us exactly that. For given values of x and y , what's z ?

Conclusion

We have done a lot in a short amount of time. Multiple linear regression models can become increasingly complex very quickly. My hope is that adding this functionality to your tools set, you’ll be able to maintain better understanding of the data and models your working with. It’s not incredibly difficult to load a model with every variable we have access to, but it does raise the question of whether it solves our objective. Does it lend the type of understanding that we set out to obtain when we engaged in the modeling process?

In a few short minutes, we’ve covered:

  • Multiple linear regression definition
  • Building a mlr model
  • Visualization/interpretation limitations
  • Using 3D plots and planes to interpret our data and models

If this was helpful, feel free to check out my other posts at datasciencelessons.com . Happy Data Science-ing!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创意,未来的工作方式

创意,未来的工作方式

方军 / 中信出版社 / 2016-11-20 / 58.00元

知识工作者已成过去,创意工作者才是未来 工作的本质是创意 纵观我们身处的世界,除了自然美景,世间或伟大或平凡的事物,几乎都是人观念革新的产物,它们多数是我们在工作过程中群体创意的产物。 从工业时代到知识时代,大多数人通过掌握新知、持续学习,获得社会的认可和回报;但进入以大数据、人工智能、机器人为标志的新时代,单纯的学习已经不能满足社会对人的要求。算法和机器人正在取代人类很多重复性......一起来看看 《创意,未来的工作方式》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具