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-predict
is just the ordinary minus sign.predict
is the function for model prediction. Read?predict
and?predict.lm
for more. The reason that you can take subtraction betweenmpg
and prediction result, is that you haveattach
the dataset. Alternatively, useAuto$mpg - predict(lm.fit, Auto)
.The second one with
-train
is for subsetting. Here is a simple example: