"Intro to Statistical Learning" Chapter 5 - Resampling p.191

176 views Asked by At

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)

1

There are 1 answers

1
Zheyuan Li On BEST ANSWER

enter image description here

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 between mpg and prediction result, is that you have attach the dataset. Alternatively, use Auto$mpg - predict(lm.fit, Auto).

The second one with -train is for subsetting. Here is a simple example:

x <- 1:5
x[-(2:3)]  ## drop element 2 to 3