Skip to content

Instantly share code, notes, and snippets.

@brentp
Created May 1, 2012 03:10
Show Gist options
  • Save brentp/2564654 to your computer and use it in GitHub Desktop.
Save brentp/2564654 to your computer and use it in GitHub Desktop.
implement f.pvalue from bioconductor's sva to compare 2 models with f-test
xs = read.table('mod4.txt', sep="\t", header=T)
y = read.table('y4.txt', sep="\t", header=T)
colnames(y) = "Survival"
df = cbind(y, xs)
print(colnames(df))
print(drop1(lm(Survival ~ SexMale + AgeChild + class_crewTRUE, data=df), test="F"))
"""
See docstrings in f_pvalue() and fp()
compare output of:
python f_pvalue.py
to R:
R --slave < compare.R
note that R's precision is limited to <2e-16, but the
f-stats and p-values are otherwise identical
"""
from numpy.linalg import lstsq
import numpy as np
from scipy.stats import fprob, f_value
def f_pvalue(y, mod, mod0):
"""
compare 2 linear models and return the p-value from the f-test
similar to drop1() in R and f.pvalue in R, bioconductor's sva package.
y is an array of response variables.
mod is a model matrix
mod0 is a simpler model matrix with a subset of columns from mod
if y is 2 dimensional, then the return values will have length equal
to the number of columns in y.
"""
if y.shape[0] != mod.shape[0] and y.shape[1] == mod.shape[0]:
y = y.T
x, x_resid, x_rank, x_singular = lstsq(mod, y)
x0, x0_resid, x0_rank, x0_singular = lstsq(mod0, y)
df0 = mod0.shape[0] - mod0.shape[1]
df = mod.shape[0] - mod.shape[1]
f_stats = f_value(x0_resid, x_resid, df0, df)
probs = fprob(1, df0, f_stats)
if len(probs) == 1:
return f_stats[0], probs[0]
else:
return f_stats, probs
def fp(y, mod, drop_cols=None):
"""
compare a model to a model with one of the columns (covariates) dropped.
uses the f-test. see f_pvalue.
if drop_cols is None, all columns are dropped in sucession.
"""
if drop_cols is None:
drop_cols = mod.columns
if not hasattr(drop_cols, "__iter__"):
drop_cols = [drop_cols]
r = {}
for dc in drop_cols:
# grab all the columns except the current
if isinstance(dc, basestring):
mod0 = mod[[c for c in mod.columns if c != dc]]
else:
mod0 = np.array(mod)[:, [x for x in range(len(mod.columns)) if x != dc]]
# compare models and save to dict.
r[dc] = f_pvalue(y, mod, mod0)
return r
if __name__ == "__main__":
mod = []
for i, row in enumerate(open('mod4.txt')):
if i == 0: continue # header
mod.append((map(int, row.split())))
mod = np.array(mod)
mod0 = mod[:, [0, 2, 3]]
ys = np.array(map(int, [x for x in open('y4.txt')][1:]))
ys = np.array((ys, ys))
print f_pvalue(ys, mod, mod0)
import pandas as pa
ys = pa.read_table('y4.txt')
mod = pa.read_table('mod4.txt')
print ys.columns
print mod.columns
# drop each column and get dict of {'column': (fscores, pvals)}
import pprint
pprint.pprint( fp(ys, mod))
print fp(ys, mod, 0)
print fp(ys, mod, "SexMale")
(Intercept) SexMale AgeChild class_crewTRUE
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 0 1 0
1 0 1 0
1 0 1 0
1 0 1 0
1 0 1 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0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