RStudio与终端R运行lm()模型结果不一致问题问询
RStudio vs Terminal R: Different Linear Model Coefficients with Full Dataset Only
I've hit a puzzling issue where running the exact same no-intercept linear model code on identical data produces drastically different coefficient results in RStudio vs. a terminal R session—and this only happens when using the full dataset. Testing smaller subsets (from 10 rows up to half the data) gives perfectly matching results across both environments. Here's the full breakdown:
Key Context
- Model Code:
(Side note:a = dputdata lm.transcprot = lm(formula = I(a$b - 0) ~ 0 + a$a)I(a$b - 0)is redundant here;a$b ~ 0 + a$aworks exactly the same) - Data:
dput()output is too large to share; password:r - Data Summary (identical in both environments):
> summary(a) a b Min. : 1.002 Min. : 0.13 1st Qu.: 5.887 1st Qu.: 5320.27 Median : 14.551 Median : 11739.61 Mean : 25.877 Mean : 20524.21 3rd Qu.: 34.424 3rd Qu.: 26430.47 Max. :136.997 Max. :116315.41
RStudio Environment & Model Output
Session Details:
> sessionInfo() R version 3.6.0 (2019-04-26) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux 9 (stretch) Matrix products: default BLAS/LAPACK: /usr/lib/libopenblasp-r0.2.19.so locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.6.0 rsconnect_0.8.13 tools_3.6.0 > find('lm') [1] "package:stats"
Model Result:
> lm.transcprot Call: lm(formula = I(a$b - 0) ~ 0 + a$a) Coefficients: a$a 462.5
Terminal R Environment & Model Output
Session Details:
> sessionInfo() R version 3.6.1 (2019-07-05) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.2 LTS Matrix products: default BLAS/LAPACK: /opt/intel/compilers_and_libraries_2019.3.199/linux/mkl/lib/intel64_lin/libmkl_rt.so locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.6.1 tools_3.6.1 > find('lm') [1] "package:stats"
Model Result:
> lm.transcprot Call: lm(formula = I(a$b - 0) ~ 0 + a$a) Coefficients: a$a 1889
Likely Causes & Fixes
- Different BLAS/LAPACK Libraries: This is the most probable root cause. RStudio uses
openblaspwhile terminal R uses Intel MKL—these are distinct linear algebra libraries with different numerical algorithms and precision handling. For large datasets, small floating-point errors can accumulate into noticeable differences in coefficient estimates. - R Version Gap: R 3.6.0 vs. 3.6.1 might have minor tweaks to
lm()or underlying math routines, though this is less impactful than the library mismatch.
To resolve this:
- Align Linear Algebra Libraries: Configure both R environments to use the same BLAS/LAPACK library (either MKL or openblasp). You can adjust this via system environment variables or your
.Rprofilefile. - Confirm Exact Data Match: Use
all.equal(a_rstudio, a_terminal)to ensure the datasets are byte-for-byte identical in both environments—even tiny floating-point discrepancies can snowball with large data. - Test Manual Calculation: Compute the no-intercept coefficient manually in both environments with
sum(a$a * a$b) / sum(a$a^2)—this will isolate whether the difference comes from the core linear algebra computation.
内容的提问来源于stack exchange,提问作者Hart Radev
相关产品推荐
相关产品推荐

