Can someone help with the usage of this line of code:
library(ISLR)
set.seed(1)
train=sample(392,196)
lm.fit=lm(mpg~horsepower,data=Auto,subset=train)
attach(Auto)
mean((mpg-predict(lm.fit,Auto))[-train]^2)
I'm looking for help on last line of code. Can someone explain what it is doing? (specifically the syntax "mpg-predict.." and usage of the "-")
For reference this comes from: "An Introduction to Statistical Learning: with Applications in R". Chapter 5 - Re-sampling. (p.191)
                        
I think you are referring to this page.
There are two
-here.The first one in
mpg-predictis just the ordinary minus sign.predictis the function for model prediction. Read?predictand?predict.lmfor more. The reason that you can take subtraction betweenmpgand prediction result, is that you haveattachthe dataset. Alternatively, useAuto$mpg - predict(lm.fit, Auto).The second one with
-trainis for subsetting. Here is a simple example: