为什么我在插入符中的“lm”中使用不同的交叉验证规范会得到相同的结果

问题描述 投票:0回答:1

我正在使用

caret
包来使用相同的数据拟合不同的模型。我对所有这些都使用交叉验证;然而,当我使用
lm
方法使用不同数量的折叠时,我得到相同的系数,我预计至少会有很小的差异。是什么原因?这是预期的吗?

感谢您的宝贵时间!

这是一个代表

library(caret)
#> Loading required package: ggplot2
#> Loading required package: lattice

{
set.seed(123)
Xs <- matrix(rnorm(300*20),nrow = 300)
Y <- rnorm(300)
data <- cbind(Xs,Y) |> as.data.frame()
}

ctrlspecs_2 <- trainControl(method="cv", number=2)
ctrlspecs_10 <- trainControl(method="cv", number=10)

set.seed(123)
model_2 <- train(Y~.,
                 data = data,
                 method = "lm",
                 trControl = ctrlspecs_2)

set.seed(123)
model_10 <- train(Y~.,
                 data = data,
                 method = "lm",
                 trControl = ctrlspecs_10)

summary(model_2)
#> 
#> Call:
#> lm(formula = .outcome ~ ., data = dat)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.5934 -0.6277 -0.0082  0.7448  2.2594 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)   
#> (Intercept) -0.044073   0.060499  -0.728  0.46692   
#> V1          -0.129567   0.065772  -1.970  0.04984 * 
#> V2          -0.002505   0.061859  -0.040  0.96773   
#> V3          -0.046897   0.059486  -0.788  0.43115   
#> V4           0.044195   0.061427   0.719  0.47245   
#> V5           0.086981   0.064085   1.357  0.17579   
#> V6           0.014166   0.061001   0.232  0.81653   
#> V7          -0.077959   0.060911  -1.280  0.20165   
#> V8           0.017661   0.065486   0.270  0.78759   
#> V9          -0.096562   0.060567  -1.594  0.11200   
#> V10          0.164024   0.060858   2.695  0.00746 **
#> V11         -0.028008   0.060869  -0.460  0.64577   
#> V12          0.034027   0.062118   0.548  0.58428   
#> V13         -0.066028   0.066681  -0.990  0.32294   
#> V14          0.142444   0.061319   2.323  0.02090 * 
#> V15         -0.129046   0.060109  -2.147  0.03267 * 
#> V16         -0.020873   0.061512  -0.339  0.73462   
#> V17          0.046835   0.063381   0.739  0.46056   
#> V18          0.035570   0.066567   0.534  0.59353   
#> V19         -0.016253   0.060039  -0.271  0.78682   
#> V20         -0.082083   0.060843  -1.349  0.17840   
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.033 on 279 degrees of freedom
#> Multiple R-squared:  0.1041, Adjusted R-squared:  0.03986 
#> F-statistic: 1.621 on 20 and 279 DF,  p-value: 0.04731
summary(model_10)
#> 
#> Call:
#> lm(formula = .outcome ~ ., data = dat)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.5934 -0.6277 -0.0082  0.7448  2.2594 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)   
#> (Intercept) -0.044073   0.060499  -0.728  0.46692   
#> V1          -0.129567   0.065772  -1.970  0.04984 * 
#> V2          -0.002505   0.061859  -0.040  0.96773   
#> V3          -0.046897   0.059486  -0.788  0.43115   
#> V4           0.044195   0.061427   0.719  0.47245   
#> V5           0.086981   0.064085   1.357  0.17579   
#> V6           0.014166   0.061001   0.232  0.81653   
#> V7          -0.077959   0.060911  -1.280  0.20165   
#> V8           0.017661   0.065486   0.270  0.78759   
#> V9          -0.096562   0.060567  -1.594  0.11200   
#> V10          0.164024   0.060858   2.695  0.00746 **
#> V11         -0.028008   0.060869  -0.460  0.64577   
#> V12          0.034027   0.062118   0.548  0.58428   
#> V13         -0.066028   0.066681  -0.990  0.32294   
#> V14          0.142444   0.061319   2.323  0.02090 * 
#> V15         -0.129046   0.060109  -2.147  0.03267 * 
#> V16         -0.020873   0.061512  -0.339  0.73462   
#> V17          0.046835   0.063381   0.739  0.46056   
#> V18          0.035570   0.066567   0.534  0.59353   
#> V19         -0.016253   0.060039  -0.271  0.78682   
#> V20         -0.082083   0.060843  -1.349  0.17840   
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.033 on 279 degrees of freedom
#> Multiple R-squared:  0.1041, Adjusted R-squared:  0.03986 
#> F-statistic: 1.621 on 20 and 279 DF,  p-value: 0.04731

identical(model_2$finalModel$coefficients,model_10$finalModel$coefficients)
#> [1] TRUE

创建于 2024-03-20,使用 reprex v2.1.0

r cross-validation lm caret
1个回答
0
投票

系数是相同的,因为

summary
为您提供了在整个数据集上拟合的线性模型的结果。

交叉验证是单独进行的,以计算模型在未见过的数据上的工作效果。您可以使用

model2$resample
model10$resample

查看交叉验证结果
© www.soinside.com 2019 - 2024. All rights reserved.