# [R] regression summary results pvalues and coefficients into a excel

David Winsemius dwinsemius at comcast.net
Mon Sep 19 04:15:22 CEST 2011

```On Sep 18, 2011, at 9:36 PM, Donald Price wrote:

> Hi All,
>
> I have run many regression analyses (14000 +) and want to collect the
> coefficients and pvalues into an excel file.  I can get the
> statements below
> to work up to step 4.  I can printout the regressionresults (sample
> output
> below).

This sounds pretty suspicious. Is this plan well thought out from a
statistical viewpoint?

>
> So my hope is to run something like step 5 and 6 and put the pvalues
> (and
> then coefficients) into an excel file.  Can anyone suggest what I am
> doing
> wrong or a better way :0

Where did you copy this code from?

>
> Thanks
>
> Don
>
>
> 1) sdata <- read.table("gene.csv", row.names=1, sep=',')
>
> 2)  headshape <- c(0.575818, 0.573874, 0.525701, 0.548490, 0.685111,
> 0.592502, 0.566001, 0.563605, 0.637906, 0.578099, 0.588142, 0.383393,
> 0.561732, 0.456134, 0.430472, 0.603143, 0.514315, 0.53328, 0.482734,
> 0.637906)
>
> 3) morphtrait <- headshape aof<-function(x){m<-
> data.frame(morphtrait,x);
> summary(lm(morphtrait~x, m))}
>
> 4) regressionresults <-apply(sdata, 1, aof)

Have you looked at str(regressionresults[])? It does not look to be
something that can be immediately accessed as though it were a matrix.

Perhaps after you extract the "coefficients" element.

>
> 5) regpvalues <- data.frame(lapply(regressionresults,
> function(x){x["Pr(>|t|)"][1:2,]}))
>

I would have guessed:

regpvalues <- data.frame(lapply(regressionresults,
function(x){ x[["coefficients"]][1:2, "Pr(>|t|)"] }

The coefficient values are the rows, and the "Pr(>|t|)"'s are the
columns.

> 6) write.table( t(regpvalues), file = "regression-
> quote = F, sep ='\t')

I doubt that t(.) will succeed. I'm not aware that "t" has a list
method

>> regressionresults
>
> \$CUST_54_PI410671829
>
> Call:
> lm(formula = morphtrait ~ x, data = m)
>
> Residuals:
>     Min       1Q   Median       3Q      Max
> -0.23217 -0.08980 -0.04592 -0.00947  1.07688
>
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.5985797  0.0364510  16.421   <2e-16 ***
> x           0.0005372  0.0011161   0.481    0.632
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.2379 on 76 degrees of freedom
> Multiple R-squared: 0.003039,   Adjusted R-squared: -0.01008
> F-statistic: 0.2316 on 1 and 76 DF,  p-value: 0.6317
>
>
> \$CUST_13662_PI410671829
>
> Call:
> lm(formula = morphtrait ~ x, data = m)
>
> Residuals:
>     Min       1Q   Median       3Q      Max
> -0.23434 -0.09000 -0.05062 -0.01427  1.06208
>
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)
> (Intercept)  0.626368   0.030173  20.759   <2e-16 ***
> x           -0.003815   0.003337  -1.143    0.256
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.2362 on 76 degrees of freedom
> Multiple R-squared: 0.01691,    Adjusted R-squared: 0.003976
> F-statistic: 1.307 on 1 and 76 DF,  p-value: 0.2565
>
>
> \$CUST_8938_PI410671829
>
> Call:
> lm(formula = morphtrait ~ x, data = m)
>
> Residuals:
>     Min       1Q   Median       3Q      Max
> -0.23424 -0.09051 -0.04721 -0.01216  1.07195
>
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.6056038  0.0368423  16.438   <2e-16 ***
> x           0.0004091  0.0021406   0.191    0.849
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.2382 on 76 degrees of freedom
> Multiple R-squared: 0.0004803,  Adjusted R-squared: -0.01267
> F-statistic: 0.03652 on 1 and 76 DF,  p-value: 0.849
>
>
> \$CUST_5773_PI410671829
>
> Call:
> lm(formula = morphtrait ~ x, data = m)
>
> Residuals:
>     Min       1Q   Median       3Q      Max
> -0.23587 -0.09139 -0.04895 -0.01037  1.07437
>
> Coefficients:
>            Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.598110   0.038504  15.534   <2e-16 ***
> x           0.001818   0.004070   0.447    0.656
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.2379 on 76 degrees of freedom
> Multiple R-squared: 0.002619,   Adjusted R-squared: -0.0105
> F-statistic: 0.1996 on 1 and 76 DF,  p-value: 0.6563
>

David Winsemius, MD
West Hartford, CT

```