Tags

, , , ,

Problem

Using R’s in-built dataset “stackloss”, develop a 95% prediction interval of the stack loss if the air flow is 70, water temperature is 22 and acid concentration is 80.

Solution

 

We apply the lm() function to a formula that describes the variable stack.loss by the variables Air.Flow, Water.Temp and Acid.Conc.
We then save the linear regression model in a new variable, say, stacklosslm

> attach(stackloss)    # attach the data frame to work space

> stacklosslm = lm(stack.loss ~ Air.Flow + Water.Temp + Acid.Conc.)

Now we store the values of variables (for which we need to make prediction) inside a new data frame, say, stockpredict.

> stockpredict = data.frame(Air.Flow=70, Water.Temp=22, Acid.Conc.=80)

Now apply the predict() function and set the predictor variable in the stockpredict argument.

Also set the interval type as “predict”, that uses 0.95 confidence level as default.

> predict(stacklosslm, stockpredict, interval=”predict”)

This produces the following prediction result:

     fit                lwr            upr 
26.50163     18.61037   34.3929 

Lastly clean the workspace

> detach(stackloss)

ANSWER (INTERPRETATION OF RESULT)

At 95% confidence interval, the value of the stack loss is between 18.61 and 34.39 when Air Flow is 70, Water Temp is 22, and Acid Concentration is 80.

I hope this tutorial was useful and easy to understand to work on R for prediction using MLR model.

Good Luck!

Manoj

Advertisements