FitGlmRowClusters.withse <- function(jrow, cnames, dat.annots.filt.mark, ncuts.cells.mark, jbin = NULL, returnobj=FALSE, with.se = FALSE){
# use Offset by size of library
# https://stats.stackexchange.com/questions/66791/where-does-the-offset-go-in-poisson-negative-binomial-regression
# fit GLM for a row of a sparse matrix, should save some space?
# pvalue by deviance goodness of fit: https://thestatsgeek.com/2014/04/26/deviance-goodness-of-fit-test-for-poisson-regression/
# offset is in log because the model says the log counts is equal to RHS
if (!is.null(nrow(jrow))){
# probably a matrix of many rows, sum them up
print(paste("Merging", nrow(jrow), "rows"))
row <- Matrix::colSums(jrow)
}
dat <- data.frame(cell = cnames, ncuts = jrow, stringsAsFactors = FALSE) %>%
left_join(., dat.annots.filt.mark, by = "cell") %>%
left_join(., ncuts.cells.mark, by = "cell")
m1.pois <- glm(ncuts ~ 1 + cluster + offset(log(ncuts.total)), data = dat, family = "poisson")
mnull.pois <- glm(ncuts ~ 1 + offset(log(ncuts.total)), data = dat, family = "poisson")
if (!returnobj){
jsum <- anova(mnull.pois, m1.pois)
pval <- pchisq(jsum$Deviance[[2]], df = jsum$Df[[2]], lower.tail = FALSE)
if (!with.se){
out.dat <- data.frame(pval = pval,
dev.diff = jsum$Deviance[[2]],
df.diff = jsum$Df[[2]],
t(as.data.frame(coefficients(m1.pois))),
stringsAsFactors = FALSE)
} else {
estimates <- summary(m1.pois)$coefficients[, "Estimate"]
names(estimates) <- make.names(paste(names(estimates), ".Estimate", sep = ""))
stderrors <- summary(m1.pois)$coefficients[, "Std. Error"]
names(stderrors) <- make.names(paste(names(stderrors), ".StdError", sep = ""))
out.dat <- data.frame(pval = pval,
dev.diff = jsum$Deviance[[2]],
df.diff = jsum$Df[[2]],
t(as.data.frame(c(estimates, stderrors))),
stringsAsFactors = FALSE)
}
if (!is.null(jbin)){
out.dat$bin <- jbin
rownames(out.dat) <- jbin
}
return(out.dat)
} else {
return(list(fit.full = m1.pois, fit.null = mnull.pois, dat.input = dat))
}
}
FitGlmRowPtime.withse <- function(jrow, cnames, dat.annots.filt.mark, ncuts.cells.mark, jbin = NULL, returnobj=FALSE, with.se = FALSE){
# use Offset by size of library
# https://stats.stackexchange.com/questions/66791/where-does-the-offset-go-in-poisson-negative-binomial-regression
# fit GLM for a row of a sparse matrix, should save some space?
# pvalue by deviance goodness of fit: https://thestatsgeek.com/2014/04/26/deviance-goodness-of-fit-test-for-poisson-regression/
# offset is in log because the model says the log counts is equal to RHS
if (!is.null(nrow(jrow))){
# probably a matrix of many rows, sum them up
print(paste("Merging", nrow(jrow), "rows"))
row <- Matrix::colSums(jrow)
}
dat <- data.frame(cell = cnames, ncuts = jrow, stringsAsFactors = FALSE) %>%
left_join(., dat.annots.filt.mark, by = "cell") %>%
left_join(., ncuts.cells.mark, by = "cell")
m1.pois <- glm(ncuts ~ 1 + ptime + offset(log(ncuts.total)), data = dat, family = "poisson")
mnull.pois <- glm(ncuts ~ 1 + offset(log(ncuts.total)), data = dat, family = "poisson")
if (!returnobj){
jsum <- anova(mnull.pois, m1.pois)
pval <- pchisq(jsum$Deviance[[2]], df = jsum$Df[[2]], lower.tail = FALSE)
if (!with.se){
out.dat <- data.frame(pval = pval,
dev.diff = jsum$Deviance[[2]],
df.diff = jsum$Df[[2]],
t(as.data.frame(coefficients(m1.pois))),
stringsAsFactors = FALSE)
} else {
estimates <- summary(m1.pois)$coefficients[, "Estimate"]
names(estimates) <- make.names(paste(names(estimates), ".Estimate", sep = ""))
stderrors <- summary(m1.pois)$coefficients[, "Std. Error"]
names(stderrors) <- make.names(paste(names(stderrors), ".StdError", sep = ""))
out.dat <- data.frame(pval = pval,
dev.diff = jsum$Deviance[[2]],
df.diff = jsum$Df[[2]],
t(as.data.frame(c(estimates, stderrors))),
stringsAsFactors = FALSE)
}
if (!is.null(jbin)){
out.dat$bin <- jbin
rownames(out.dat) <- jbin
}
return(out.dat)
} else {
return(list(fit.full = m1.pois, fit.null = mnull.pois, dat.input = dat))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.