[R] create data frame with coefficients from many regressions
arun
smartpink111 at yahoo.com
Mon Jul 22 22:18:36 CEST 2013
set.seed(28)
dat1<- as.data.frame(matrix(sample(1:20,100,replace=TRUE),ncol=10))
set.seed(49)
dat2<- as.data.frame(matrix(sample(40:80,100,replace=TRUE),ncol=10))
sapply(seq_len(ncol(dat1)),function(i) {x1<- summary(lm(dat2[,i]~dat1[,i]));x1$coef[,1]})
# [,1] [,2] [,3] [,4] [,5] [,6]
#(Intercept) 50.3768788 53.5300207 65.2972973 55.6530015 58.5158172 79.368165
#dat1[, i] 0.4770829 0.2767426 -0.4554849 0.3089312 0.7785589 -1.193601
# [,7] [,8] [,9] [,10]
#(Intercept) 59.8130393 67.089662 74.593072 66.39938809
#dat1[, i] -0.4659636 -1.498945 -1.221709 0.05624853
as.data.frame(sapply(seq_len(ncol(dat1)),function(i) {x1<- summary(lm(dat2[,i]~dat1[,i]));x1$coef[,1]}))
A.K.
----- Original Message -----
From: iza.ch1 <iza.ch1 at op.pl>
To: r-help at r-project.org
Cc:
Sent: Monday, July 22, 2013 3:11 PM
Subject: [R] create data frame with coefficients from many regressions
> Hi !
>
> I want to ask if somebody knows the way to create data frame with coefficients from many regressions
> I regress the first column from ret against the first columns from median, then the second with the second and so on.
> This is the code used for regression
>
> i<-1:6
> lapply(seq_len(ncol(ret)),function(i) {lm(ret[,i]~median[,i])}
>
> I get 6 results for each regression
>
> [[1]]
>
> Call:
> lm(formula = ret[, i] ~ median[, i])
>
> Coefficients:
> (Intercept) median[, i]
> 0 1
>
>
> [[2]]
>
> Call:
> lm(formula = ret[, i] ~ median[, i])
>
> Coefficients:
> (Intercept) median[, i]
> -1.411e-17 1.000e+00
>
> now I would like to create a data frame with intercepts which looks like it
>
> [[1]] [[2]]
> Intercept
> median
>
> I tried to use ddply command but it does not work. I will be very grateful for the hint :)
>
> Thank you in advance
>
>
______________________________________________
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.
More information about the R-help
mailing list