Skip to content

Instantly share code, notes, and snippets.

@vankesteren
Last active February 23, 2022 13:25
Show Gist options
  • Select an option

  • Save vankesteren/41c1ef5467520b298099cc135cdef4a9 to your computer and use it in GitHub Desktop.

Select an option

Save vankesteren/41c1ef5467520b298099cc135cdef4a9 to your computer and use it in GitHub Desktop.
fast vertically partitioned glms
# Setup
set.seed(45)
N <- 1000
## alice
Pa <- 20
Xa <- matrix(rnorm(N*Pa), N)
## bob
Pb <- 15
Xb <- matrix(rnorm(N*Pb), N)
## full data
X <- cbind(Xa, Xb)
## outcome
beta <- rnorm(Pa+Pb)
eta <- X%*%beta
y <- vapply(binomial()$linkinv(eta), \(pr) rbinom(1, 1, prob = pr), 1L)
# full model
fit_full <- glm(y ~ X, family = binomial())
# modeling from alice's perspective
## Bob generates random key matrix Rb
Rb <- matrix(runif(Pb*Pb), Pb)
## Bob ensures Rb is invertible
solve(Rb)
## Bob obscures his data with this key matrix
Vb <- Xb%*%Rb
## Alice obtains Vb and creates new dataset Za
Za <- cbind(Xa, Vb)
## Alice proceeds to do the glm as normal but with Za
fit_alice <- glm(y ~ Za, family = binomial())
## now all of alice's parameters and standard errors are correct
comparison <- cbind(summary(fit_full)$coef, summary(fit_alice)$coef)
comparison[1:21,]
## only bob's part is different
comparison[23:36,]
@vankesteren

vankesteren commented Feb 23, 2022 •

Copy link
Copy Markdown
Author

Alice's parameters

                                  Full data                     |                     Alice's view
                 Estimate Std. Error       z value     Pr(>|z|) |      Estimate Std. Error       z value     Pr(>|z|)
(Intercept)  0.0004074971  0.1518837   0.002682956 9.978593e-01 |  0.0004074971  0.1518837   0.002682956 9.978593e-01
X1          -0.8319147376  0.1530671  -5.434966819 5.480663e-08 | -0.8319147376  0.1530671  -5.434966819 5.480663e-08
X2           1.5022530380  0.2131621   7.047466319 1.822049e-12 |  1.5022530380  0.2131621   7.047466319 1.822049e-12
X3          -2.4263064555  0.2518125  -9.635368761 5.668809e-22 | -2.4263064555  0.2518125  -9.635368761 5.668809e-22
X4           0.1381812749  0.1604397   0.861266349 3.890914e-01 |  0.1381812749  0.1604397   0.861266349 3.890914e-01
X5           0.6889020477  0.1619048   4.254980954 2.090668e-05 |  0.6889020477  0.1619048   4.254980954 2.090668e-05
X6           1.6176920746  0.1993881   8.113282935 4.927017e-16 |  1.6176920746  0.1993881   8.113282935 4.927017e-16
X7           1.6380165676  0.1926616   8.502041599 1.862850e-17 |  1.6380165676  0.1926616   8.502041599 1.862850e-17
X8           0.7727351030  0.1539915   5.018037667 5.220194e-07 |  0.7727351030  0.1539915   5.018037667 5.220194e-07
X9           1.5347563279  0.1965724   7.807590067 5.829182e-15 |  1.5347563279  0.1965724   7.807590067 5.829182e-15
X10         -1.3796015488  0.1870127  -7.377047570 1.618382e-13 | -1.3796015488  0.1870127  -7.377047570 1.618382e-13
X11          1.5756228031  0.1952239   8.070848039 6.981156e-16 |  1.5756228031  0.1952239   8.070848039 6.981156e-16
X12          0.9788053061  0.1742350   5.617732138 1.934801e-08 |  0.9788053061  0.1742350   5.617732138 1.934801e-08
X13          2.2160039615  0.2420989   9.153299374 5.522069e-20 |  2.2160039615  0.2420989   9.153299374 5.522069e-20
X14         -3.0676614300  0.2931089 -10.465944028 1.238359e-25 | -3.0676614300  0.2931089 -10.465944028 1.238359e-25
X15          0.7344238588  0.1627480   4.512644974 6.402414e-06 |  0.7344238588  0.1627480   4.512644974 6.402414e-06
X16         -0.4100632504  0.1600796  -2.561620909 1.041850e-02 | -0.4100632504  0.1600796  -2.561620909 1.041850e-02
X17          0.6561707765  0.1544260   4.249094526 2.146363e-05 |  0.6561707765  0.1544260   4.249094526 2.146363e-05
X18         -2.1657868875  0.2363239  -9.164484989 4.978412e-20 | -2.1657868875  0.2363239  -9.164484989 4.978412e-20
X19         -0.4651322306  0.1558312  -2.984846486 2.837207e-03 | -0.4651322306  0.1558312  -2.984846486 2.837207e-03
X20         -0.2424752213  0.1578107  -1.536493882 1.244173e-01 | -0.2424752213  0.1578107  -1.536493882 1.244173e-01

Bob's parameters

                      Full data                    |                 Alice's view
       Estimate Std. Error    z value     Pr(>|z|) |   Estimate Std. Error    z value     Pr(>|z|)
X21 -1.31135765  0.1789991 -7.3260583 2.370206e-13 |  8.5400432  3.2317390  2.6425535 8.228346e-03
X22  0.13648248  0.1544792  0.8835007 3.769658e-01 | 16.2371391  2.2907443  7.0881499 1.359167e-12
X23 -1.39716938  0.1935950 -7.2169700 5.315869e-13 | -5.9527481  1.1473661 -5.1881853 2.123532e-07
X24 -0.12236364  0.1513865 -0.8082860 4.189259e-01 |  1.0055454  1.4351290  0.7006655 4.835118e-01
X25  1.61429443  0.2099696  7.6882278 1.491870e-14 |  6.2624693  2.9245438  2.1413492 3.224589e-02
X26  0.07934102  0.1420575  0.5585135 5.764938e-01 | -8.4314301  1.4736370 -5.7215109 1.055809e-08
X27  1.07005244  0.1864757  5.7382928 9.563568e-09 | -9.9584345  1.6443330 -6.0562153 1.393615e-09
X28  0.68309050  0.1577600  4.3299337 1.491543e-05 |  3.0531948  0.4786538  6.3787122 1.785832e-10
X29  0.52623162  0.1583618  3.3229712 8.906410e-04 | -5.1493625  0.7050478 -7.3035649 2.802417e-13
X30  2.06962166  0.2317069  8.9320673 4.181211e-19 | -4.8588908  2.9381125 -1.6537457 9.817920e-02
X31 -1.62463459  0.2030865 -7.9997181 1.247044e-15 |  7.2830329  1.9236203  3.7861073 1.530256e-04
X32 -1.16593062  0.1727581 -6.7489205 1.489492e-11 | -2.1648042  0.7924493 -2.7317890 6.299147e-03
X33  0.12929881  0.1490589  0.8674344 3.857040e-01 |  3.1089076  2.0581236  1.5105544 1.309020e-01
X34  1.24152104  0.1801729  6.8907217 5.551005e-12 | -5.7399974  1.4736743 -3.8950245 9.818881e-05
X35  0.34542635  0.1514734  2.2804428 2.258144e-02 | -5.541401  1.4915710 -3.7151440 2.030880e-04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment