1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 "github.com/kshedden/statmodel/glm"
12 "github.com/kshedden/statmodel/statmodel"
15 var glmConfig = &glm.Config{
16 Family: glm.NewFamily(glm.BinomialFamily),
21 // Logistic regression.
23 // onehot is the observed outcome, in same order as sampleInfo, but
24 // shorter because it only has entries for samples with
26 func pvalueGLM(sampleInfo []sampleInfo, onehot []bool, nPCA int) float64 {
27 pcaNames := make([]string, 0, nPCA)
28 data := make([][]statmodel.Dtype, 0, nPCA)
29 for pca := 0; pca < nPCA; pca++ {
30 series := make([]statmodel.Dtype, 0, len(sampleInfo))
31 for _, si := range sampleInfo {
33 series = append(series, si.pcaComponents[pca])
36 data = append(data, series)
37 pcaNames = append(pcaNames, fmt.Sprintf("pca%d", pca))
40 variant := make([]statmodel.Dtype, 0, len(sampleInfo))
41 outcome := make([]statmodel.Dtype, 0, len(sampleInfo))
43 for _, si := range sampleInfo {
46 variant = append(variant, 1)
48 variant = append(variant, 0)
51 outcome = append(outcome, 1)
53 outcome = append(outcome, 0)
58 data = append(data, variant, outcome)
60 dataset := statmodel.NewDataset(data, append(pcaNames, "variant", "outcome"))
61 model, err := glm.NewGLM(dataset, "outcome", pcaNames, glmConfig)
65 resultCov := model.Fit()
66 logCov := resultCov.LogLike()
67 model, err = glm.NewGLM(dataset, "outcome", append([]string{"variant"}, pcaNames...), glmConfig)
71 resultComp := model.Fit()
72 logComp := resultComp.LogLike()
73 return chisquared.Survival(-2 * (logCov - logComp))