在 r 中拟合模型时,“~”之间有什么区别?和“~1”?

When fitting a model in r, what is the difference between "~." and "~1"?

例如,我正在做一些生存分析,想要拟合一个模型:

fit1 <- coxph(formula = Surv(week, arrest)~., data = rossi)

我知道那个“~”。是一种将所有协变量包含在拟合中的 shorthand 方法。但是,有这样的情况

fit1 <- coxph(formula = Surv(week, arrest)~1, data = rossi)

这里使用了“~1”,尽管我不太确定 1 的作用。如果有的话,这两者有什么区别?

我不确定你关注的是什么,但正如你所说,~. 表示使用每个变量的模型。

并且,~1表示不使用变量。您的模型中只会包含常数系数。

使用GlobalDeviance::Rossi数据集,两个模型之间存在非常明显的差异。

coxph(formula = Surv(week, arrest) ~ ., data = Rossi)

                   coef  exp(coef)   se(coef)       z        p
fin          -3.832e+01  2.285e-17  4.471e+01  -0.857  0.39140
age           6.526e-01  1.920e+00  1.129e-01   5.778 7.56e-09
race          2.640e+01  2.914e+11  1.160e+00  22.756  < 2e-16
wexp         -1.341e+01  1.503e-06  1.155e+00 -11.610  < 2e-16
mar          -2.926e+01  1.965e-13  3.204e+02  -0.091  0.92723
paro         -7.623e+00  4.888e-04  1.002e+00  -7.610 2.74e-14
prio         -5.554e-01  5.739e-01  3.594e-01  -1.545  0.12224
educ          4.775e+00  1.186e+02  1.002e+00   4.768 1.86e-06
emp1          2.831e+01  1.977e+12  1.002e+00  28.262  < 2e-16
emp2         -1.586e+00  2.048e-01  1.002e+00  -1.583  0.11349
emp3          1.206e+01  1.724e+05  1.002e+00  12.036  < 2e-16
emp4          2.157e+01  2.341e+09  1.002e+00  21.536  < 2e-16
emp5         -3.970e+01  5.763e-18  1.002e+00 -39.625  < 2e-16
emp6         -4.786e+00  8.350e-03  1.155e+00  -4.144 3.42e-05
emp7          1.026e+01  2.856e+04  1.002e+00  10.242  < 2e-16
emp8          2.427e+01  3.462e+10  1.002e+00  24.225  < 2e-16
emp9         -2.961e+01  1.376e-13  1.002e+00 -29.562  < 2e-16
emp10         6.266e+00  5.264e+02  1.002e+00   6.255 3.98e-10
emp11         1.036e+01  3.142e+04  1.002e+00  10.337  < 2e-16
emp12        -5.055e+00  6.375e-03  1.002e+00  -5.046 4.50e-07
emp13        -1.462e+01  4.452e-07  1.002e+00 -14.599  < 2e-16
emp14         4.907e+00  1.352e+02  1.002e+00   4.898 9.67e-07
emp15        -2.577e+01  6.427e-12  1.002e+00 -25.725  < 2e-16
emp16         6.024e+00  4.132e+02  1.002e+00   6.013 1.82e-09
emp17        -1.644e+01  7.234e-08  1.002e+00 -16.413  < 2e-16
emp18         3.171e+01  5.900e+13  1.002e+00  31.652  < 2e-16
emp19         1.364e+01  8.397e+05  1.002e+00  13.617  < 2e-16
emp20        -1.646e+01  7.093e-08  1.002e+00 -16.432  < 2e-16
emp21        -1.895e-02  9.812e-01  1.002e+00  -0.019  0.98491
emp22        -1.249e+01  3.755e-06  1.002e+00 -12.470  < 2e-16
emp23        -3.892e+01  1.252e-17  1.170e+00 -33.252  < 2e-16
emp24         6.774e+01  2.621e+29  1.002e+00  67.618  < 2e-16
emp25        -4.181e+00  1.529e-02  1.002e+00  -4.173 3.00e-05
emp26         2.336e+01  1.396e+10  1.002e+00  23.318  < 2e-16
emp27        -7.105e+01  1.388e-31  1.002e+00 -70.926  < 2e-16
emp28         8.149e+00  3.462e+03  1.002e+00   8.135 4.12e-16
emp29        -2.850e+00  5.785e-02  1.002e+00  -2.845  0.00444
emp30         2.439e+01  3.910e+10  1.002e+00  24.346  < 2e-16
emp31        -3.805e+01  2.990e-17  1.002e+00 -37.981  < 2e-16
emp32         3.836e+01  4.575e+16  1.002e+00  38.294  < 2e-16
emp33        -2.804e+00  6.056e-02  1.002e+00  -2.799  0.00512
emp34         2.236e+01  5.162e+09  1.170e+00  19.109  < 2e-16
emp35        -8.057e+00  3.167e-04  1.170e+00  -6.884 5.80e-12
emp36        -3.150e+01  2.078e-14  1.171e+00 -26.914  < 2e-16
emp37         4.106e+01  6.825e+17  1.171e+00  35.081  < 2e-16
emp38         3.176e+00  2.394e+01  1.170e+00   2.713  0.00667
emp39        -2.956e+01  1.453e-13  1.170e+00 -25.254  < 2e-16
emp40        -7.740e+00  4.349e-04  1.002e+00  -7.727 1.10e-14
emp41         2.148e+01  2.129e+09  1.002e+00  21.441  < 2e-16
emp42        -2.305e+01  9.733e-11  1.171e+00 -19.694  < 2e-16
emp43         7.306e+00  1.489e+03  1.170e+00   6.242 4.32e-10
emp44         1.620e+01  1.087e+07  1.170e+00  13.842  < 2e-16
emp45         4.468e+01  2.535e+19  1.170e+00  38.174  < 2e-16
emp46        -5.447e+01  2.204e-24  1.170e+00 -46.542  < 2e-16
emp47         1.878e+01  1.426e+08  1.171e+00  16.040  < 2e-16
emp48        -1.159e+01  9.228e-06  1.171e+00  -9.904  < 2e-16
emp49        -2.961e+01  1.382e-13  1.171e+00 -25.296  < 2e-16
emp50         3.624e+01  5.468e+15  1.171e+00  30.958  < 2e-16
emp51        -1.418e-01  8.678e-01  1.171e+00  -0.121  0.90357
emp52        -1.442e+01  5.448e-07  1.171e+00 -12.321  < 2e-16
n.work.weeks  0.000e+00  1.000e+00  2.341e-02   0.000  1.00000

Likelihood ratio test=34.75  on 61 df, p=0.9973
n= 322, number of events= 4 
   (110 observations deleted due to missingness)

.

coxph(formula = Surv(week, arrest)~1, data = Rossi)

Null model
  log likelihood= -675.3806 
  n= 432 

通过拟合线性回归可能更容易理解差异。

lm(Species ~ ., data = iris)

Call:
lm(formula = Species ~ ., data = iris)

Coefficients:
 (Intercept)  Sepal.Length   Sepal.Width  Petal.Length   Petal.Width  
     1.18650      -0.11191      -0.04008       0.22865       0.60925 

lm(Species ~ 1, data = iris)

Coefficients:
(Intercept)  
          2