Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As pointed out above, this is a bit tricky for a multi-dimensional model.</p> <p>Get some data (you neglected to include a reproducible example: see <a href="http://tinyurl.com/reproducible-000" rel="nofollow noreferrer">http://tinyurl.com/reproducible-000</a> ...)</p> <pre><code>library(foreign) dat &lt;- read.arff(url("http://www.cs.umb.edu/~rickb/files/UCI/autos.arff")) </code></pre> <p>Split into training and test data sets:</p> <pre><code>train &lt;- dat[1:150,] test &lt;- dat[151:nrow(dat),] </code></pre> <p>The variable names are a bit awkward for R (the dashes are interpreted as minus operators, so we have to use back-quotes to protect the names):</p> <pre><code>fit &lt;- lm(`city-mpg` ~ `peak-rpm`+horsepower+`wheel-base`,data=train) temp_test &lt;- predict(fit,test) </code></pre> <p>Plot the predictions vs peak RPM:</p> <pre><code>par(las=1,bty="l") ## cosmetic plot(test[["peak-rpm"]],temp_test,xlab="peak rpm",ylab="predicted") </code></pre> <p>In order to add the line, we have to adjust the intercept according to some baseline values of the other parameters: we'll use the mean (another alternative is to center all the predictor variables before fitting the model):</p> <pre><code>cf &lt;- coef(fit) abline(a=cf["(Intercept)"]+ mean(test$horsepower)*cf["horsepower"]+ mean(test$`wheel-base`)*cf["`wheel-base`"], b=coef(fit)["`peak-rpm`"]) </code></pre> <p>Another way to do this is to use <code>predict()</code>:</p> <pre><code>newdat &lt;- with(test, data.frame(horsepower=mean(horsepower), "wheel-base"=mean(`wheel-base`), "peak-rpm"=seq(min(`peak-rpm`), max(`peak-rpm`), length=41), check.names=FALSE)) newdat["city-mpg"] &lt;- predict(fit,newdat) with(newdat,lines(`peak-rpm`,`city-mpg`,col=4)) </code></pre> <p>(41 points is silly for a straight line -- we could have used just 2 -- but will work well if you want to plot something curved, like confidence intervals or a nonlinear fit.)</p> <p>Alternatively you could just fit the marginal model, but the actual fitted line is somewhat different (it will only be the same if all the predictors are orthogonal to each other):</p> <pre><code>fit2 &lt;- lm(`city-mpg` ~ `peak-rpm`,data=train) abline(fit2,col="red") </code></pre> <p><img src="https://i.stack.imgur.com/Ty90y.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload