iza.ch1
2013-Jul-22 19:11 UTC
[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 > >
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.
Rui Barradas
2013-Jul-22 21:04 UTC
[R] create data frame with coefficients from many regressions
Hello, Using Arun's data example, you can also do the following. 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)) lm.list <- lapply(seq_len(ncol(dat1)), function(i) lm(dat1[,i] ~ dat2[,i])) do.call(cbind, lapply(lm.list, coef)) # object of class "matrix" Hope this helps, Rui Barradas Em 22-07-2013 20:11, iza.ch1 escreveu:> >> 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. >