r - Obtain t-statistic for regression coefficients of an “mlm” object returned by `lm()` -
i've used lm()
fit multiple regression models, multiple (~1 million) response variables in r. eg.
allmodels <- lm(t(responsevariablesmatrix) ~ modelmatrix)
this returns object of class "mlm", huge object containing models. want t-statistic first coefficient in each model, can using summary(allmodels)
function, slow on large data , returns lot of unwanted info too.
is there faster way of calculating t-statistic
manually, might faster using summary()
function
thanks!
you can hack summary.lm() function bits need , leave rest.
if have
nvariables <- 5 nobs <- 15 y <- rnorm(nobs) x <- matrix(rnorm(nvariables*nobs),nrow=nobs) allmodels <-lm(y~x)
then code lm.summary() function excess baggage removed (note, error handling has been removed well).
p <- allmodels$rank rdf <- allmodels$df.residual qr <- allmodels$qr n <- nrow(qr$qr) p1 <- 1l:p r <- allmodels$residuals f <- allmodels$fitted.values w <- allmodels$weights mss <- if (attr(allmodels$terms, "intercept")) sum((f - mean(f))^2) else sum(f^2) rss <- sum(r^2) resvar <- rss/rdf r <- chol2inv(qr$qr[p1, p1, drop = false]) se <- sqrt(diag(r) * resvar) est <- allmodels$coefficients[qr$pivot[p1]] tval <- est/se
tval
vector of t statistics give
summary(allmodels)$coefficients[,3]
if have problems on large model might want rewrite code keeps fewer objects compounding multiple lines/assignments fewer lines.
hacky solution know. fast possible. suppose neater put lines of code function well.
Comments
Post a Comment