我正在从《统计学习导论:基于R语言的应用》自学R语言。我确信这两段代码应该得到相同的平均值,但是我得到了截然不同的结果。请问有谁可以帮我找出为什么我没有得到相同的消息?看起来第一段代码块是错误的。这些代码来自汽车数据集。我的预测和书中的预测是不同的。然而,这两者所训练的索引是相同的。
第一段代码块(我的代码)
set.seed(1)
train_index = sample (392, 196)
Auto$index = c(1:nrow(Auto))
train_df = Auto[train_index,]
test_df = anti_join(Auto, train_df, by="index")
attach(train_df)
lm.fit = lm(mpg ~ horsepower)
predictions = predict(lm.fit, horsepower = test_df$horsepower)
mean((test_df$mpg - predictions)^2)
第二部分(书中代码 - 统计学习导论:R语言实现)
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)