# Re: [R] Regression Problem for loop

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Thu, 27 Nov 2008 15:30:35 -0500

Dear ales,
Try this:

# Data
# set.seed(123)
mat=matrix(rnorm(36),ncol=6)
colnames(mat)=paste('x',1:6,sep="")
res=rnorm(6)

# All info for coefficients. First 4 columns # correspond to the intercept and the next for to the slope
t(apply(mat,2,function(x){
sm=summary(lm(res~x))\$coeff
res=matrix(sm,ncol=prod(dim(sm)))
names(res)=rep(colnames(sm),nrow(sm))
res
}
)
)

Estimate Std. Error t value Pr(>|t|) Estimate Std. Error t value Pr(>|t|)
x1 -0.3740663 0.3416852 0.2586267 0.1991565 -1.4463559 1.7156617 0.2216156 0.16136742
x2 -0.3972439 -0.5951867 0.2622604 0.3475569 -1.5146930 -1.7124872 0.2044207 0.16196830
x3 -0.1486117 -0.4973452 0.2063296 0.1890952 -0.7202638 -2.6301317 0.5112057 0.05817818
x4 -0.2408166 0.2314570 0.3159543 0.3260189 -0.7621881 0.7099497 0.4884137 0.51693194
x5 -0.4113849 0.4442002 0.1950218 0.1539688 -2.1094303 2.8850004 0.1025563 0.04478734
x6 -0.2652903 0.1900581 0.3264551 0.4945158 -0.8126393 0.3843318 0.4620218 0.72029059

HTH, Jorge

On Thu, Nov 27, 2008 at 4:03 AM, ales grill <alesgrill_at_gmail.com> wrote:

> Dear all,
> I have wrote a code for a linear regression. I want to
> write a loop for so, that I can get estimate for pavlues for six
> predictors.
> But I am getting for estmate for only last one. How can I get pvalues for
> all my predictors in a loop??
>
> Thanks
> Ales
>
>
>
>
> > mat<-matrix(rnorm(36),nrow=6)
> > mat
> [,1] [,2] [,3] [,4]
> [,5] [,6]
> [1,] 1.10536338 -0.7613770 -1.7100569 -1.8762241 -0.36579280 0.6465219
> [2,] -1.34836804 -0.2174270 -0.1153477 -0.1727683 -1.88406206 1.7484955
> [3,] 0.96814418 -2.1483727 0.5839668 -1.2361659 0.04592844 1.9937995
> [4,] 0.01960219 -1.2339691 0.8290761 0.1002795 -0.15952881 0.3969251
> [5,] 1.62343073 1.3741222 -1.2045854 0.4180127 -0.09898615 1.3575119
> [6,] -0.95260509 -0.1522824 -1.4257526 1.0057412 -1.20068336 -0.4306761
> > res<-rnorm(6)
> > res
> [1] 0.2045252 -0.9824761 0.7727004 0.6439993 1.8005737 1.0167214
> >
> > pval<-NULL
> >
> > for(i in c(1:6))
> + {
> + reg<-lm(res~mat[,i])
> + reg
> + pval[i]<-reg\$p.value
> + }
> > pval
> NULL
> > reg
>
> Call:
> lm(formula = res ~ mat[, i])
> Coefficients:
> (Intercept) mat[, i]
> 0.8195 -0.2557
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 27 Nov 2008 - 20:34:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 28 Nov 2008 - 00:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.