1. 程式人生 > >Linear Regression in R

Linear Regression in R

In this post you will discover 4 recipes for non-linear regression in R.

There are many advanced methods you can use for non-linear regression, and these recipes are but a sample of the methods you could use.

non-linear regression

Non-Linear Regression
Photo by

Steve Jurvetson, some rights reserved

Each example in this post uses the longley dataset provided in the datasets package that comes with R. The longley dataset describes 7 economic variables observed from 1947 to 1962 used to predict the number of people employed yearly.

Multivariate Adaptive Regression Splines

Multivariate Adaptive Regression Splines (MARS) is a non-parametric regression method that models multiple nonlinearities in data using hinge functions (functions with a kink in them).

Multivariate Adaptive Regression Splines in R R
123456789101112131415 # load the packagelibrary(earth)# load datadata(longley)# fit modelfit<-earth(Employed~.,longley)# summarize the fitsummary(fit)# summarize the importance of input variablesevimp(fit)# make predictionspredictions<-predict(fit,longley)# summarize accuracymse<-mean((longley$Employed-predictions)^2)print(mse)

Learn more about the earth function and the earth package.

Support Vector Machine

Support Vector Machines (SVM) are a class of methods, developed originally for classification, that find support points that best separate classes. SVM for regression is called Support Vector Regression (SVM).

Support Vector Machine in R R
12345678910111213 # load the packagelibrary(kernlab)# load datadata(longley)# fit modelfit<-ksvm(Employed~.,longley)# summarize the fitsummary(fit)# make predictionspredictions<-predict(fit,longley)# summarize accuracymse<-mean((longley$Employed-predictions)^2)print(mse)

Learn more about the ksvm function and the kernlab package.

Need more Help with R for Machine Learning?

Take my free 14-day email course and discover how to use R on your project (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

k-Nearest Neighbor

The k-Nearest Neighbor (kNN) does not create a model, instead it creates predictions from close data on-demand when a prediction is required. A similarity measure (such as Euclidean distance) is used to locate close data in order to make predictions.

k-Nearest Neighbor in R R
12345678910111213 # load the packagelibrary(caret)# load datadata(longley)# fit modelfit<-knnreg(longley[,1:6],longley[,7],k=3)# summarize the fitsummary(fit)# make predictionspredictions<-predict(fit,longley[,1:6])# summarize accuracymse<-mean((longley$Employed-predictions)^2)print(mse)

Learn more about the knnreg function and the caret package.

Neural Network

A Neural Network (NN) is a graph of computational units that recieve inputs and transfer the result into an output that is passed on. The units are ordered into layers to connect the features of an input vector to the features of an output vector. With training, such as the Back-Propagation algorithm, neural networks can be designed and trained to model the underlying relationship in data.

Neural Network in R R
123456789101112131415 # load the packagelibrary(nnet)# load datadata(longley)x<-longley[,1:6]y<-longley[,7]# fit modelfit<-nnet(Employed~.,longley,size=12,maxit=500,linout=T,decay=0.01)# summarize the fitsummary(fit)# make predictionspredictions<-predict(fit,x,type="raw")# summarize accuracymse<-mean((y-predictions)^2)print(mse)

Learn more about the nnet function and the nnet package.

Summary

In this post you discovered 4 non-linear regression methods with recipes that you can copy-and-paste for your own problems.

For more information see Chapter 7 of Applied Predictive Modeling by Kuhn and Johnson that provides an excellent introduction to non-linear regression with R for beginners.


Frustrated With Your Progress In R Machine Learning?

Master Machine Learning With R

Develop Your Own Models in Minutes

…with just a few lines of R code

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, build models, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.


相關推薦

Linear Regression in R with Decision Trees

Tweet Share Share Google Plus In this post, you will discover 8 recipes for non-linear regressio

Linear Regression in R

Tweet Share Share Google Plus In this post you will discover 4 recipes for non-linear regression

Logistic regression in R

Datacamp Learning Logistic regression in R Building simple logistic regression models The donors dataset contains 93,462 examples of peop

Linear Regression in the Wild

In one of my job interviews for a data scientist position, I was given a home assignment I'd like to share with you. The interviewer sent me a CSV file con

Simple and Multiple Linear Regression in Python

Linear Regression in PythonThere are two main ways to perform linear regression in Python — with Statsmodels and scikit-learn. It is also possible to use t

Linear Regression in 6 lines of Python

Today to perform Linear Regression quickly, we will be using the library scikit-learn. If you don’t have it already you can install it using pip:pip instal

Simple Linear Regression in Python

Simple Linear Regression in Python“If you can’t explain it simply, you don’t understand it well enough.”Simple linear regression is a statistical method th

Penalized Regression in R

Tweet Share Share Google Plus In this post you will discover 3 recipes for penalized regression

Linear Classification in R with Decision Trees

Tweet Share Share Google Plus In this post you will discover 7 recipes for non-linear classifica

Linear Classification in R

Tweet Share Share Google Plus In this post you will discover recipes for 3 linear classification

Least Squares Method in Linear Regression

linear regression This is my study notes in machine learning, writing articles in English because I want to improve my writing skills. Anyway, t

Linear and Logistic Regression in TensorFlow

Linear and Logistic Regression in TensorFlow   Graphs and sessions TF Ops: constants, variables, functions TensorBoard Lazy loading Linear Re

OLS and Logistic Regression Models in R

OLS and Logistic Regression Models in RWe use linear models primarily to analyse cross-sectional data; i.e. data collected at one specific point in time ac

二、Linear Regression 練習(轉載)

hold off int 必須 html 移動 根據 which win from 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2961660.html 前言   本文是多元線性回歸的練習,這裏練習的

Ng第二課:單變量線性回歸(Linear Regression with One Variable)

dll oba vcf 更多 dba cfq dpf gis avd 二、單變量線性回歸(Linear Regression with One Variable) 2.1 模型表示 2.2 代價函數 2.3 代價函數的直觀理解 2.4 梯度下降

從零單排入門機器學習:線性回歸(linear regression)實踐篇

class rom enter instr function ont 線性 gin 向量 線性回歸(linear regression)實踐篇 之前一段時間在coursera看了Andrew ng的機器學習的課程,感覺還不錯,算是入門了。這次打算以該課程的作業

ufldl學習筆記與編程作業:Linear Regression(線性回歸)

cal bug war 環境 art link 行數 ear sad ufldl學習筆記與編程作業:Linear Regression(線性回歸) ufldl出了新教程,感覺比之前的好。從基礎講起。系統清晰,又有編程實踐。在deep learning高質量群裏

intersect for multiple vectors in R

con span osi library tar other and pos intersect Say you have a <- c(1,3,5,7,9) b <- c(3,6,8,9,10) c <- c(2,3,4,5,7,9) A stra

機器學習基石筆記-Lecture 9 Linear regression

空間 -i 結果 thumb src 9.png http regress containe 線性回歸的任務是對於一個輸入,給出輸出的實數,保證和真實輸出相差越小越好。因為假設空間是線性的,所以最後的g會是直線或者平面。 通常的誤差衡量方法是使用平方誤差 接下來的問題是

機器學習經典算法具體解釋及Python實現--線性回歸(Linear Regression)算法

ica single 方便 最好的 而且 == show des fun (一)認識回歸 回歸是統計學中最有力的工具之中的一個。機器學習監督學習算法分為分類算法和回歸算法兩種,事實上就是依據類別標簽分布類型為離散型、連續性而定義的。顧名思義。分類算法用於離散型分布