Book Image

Machine Learning with R Cookbook

By : Yu-Wei, Chiu (David Chiu)
Book Image

Machine Learning with R Cookbook

By: Yu-Wei, Chiu (David Chiu)

Overview of this book

<p>The R language is a powerful open source functional programming language. At its core, R is a statistical programming language that provides impressive tools to analyze data and create high-level graphics.</p> <p>This book covers the basics of R by setting up a user-friendly programming environment and performing data ETL in R. Data exploration examples are provided that demonstrate how powerful data visualization and machine learning is in discovering hidden relationships. You will then dive into important machine learning topics, including data classification, regression, clustering, association rule mining, and dimension reduction.</p>
Table of Contents (21 chapters)
Machine Learning with R Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Resources for R and Machine Learning
Dataset – Survival of Passengers on the Titanic
Index

Using linear regression to predict unknown values


With a fitted regression model, we can apply the model to predict unknown values. For regression models, we can express the precision of prediction with a prediction interval and a confidence interval. In the following recipe, we introduce how to predict unknown values under these two measurements.

Getting ready

You need to have completed the previous recipe by computing the linear model of the x and y1 variables from the quartet dataset.

How to do it...

Perform the following steps to predict values with linear regression:

  1. Fit a linear model with the x and y1 variables:

    > lmfit = lm(y1~x, Quartet)
    
  2. Assign values to be predicted into newdata:

    > newdata = data.frame(x = c(3,6,15))
    
  3. Compute the prediction result using the confidence interval with level set as 0.95:

    > predict(lmfit, newdata, interval="confidence", level=0.95)
            fit      lwr       upr
    1  4.500364 2.691375  6.309352
    2  6.000636 4.838027  7.163245
    3 10.501455 8.692466 12...