[R] Looping an lapply linear regression function
arun
smartpink111 at yahoo.com
Thu Sep 5 18:49:16 CEST 2013
HI,
May be this helps:
set.seed(28)
dat1<- setNames(as.data.frame(matrix(sample(1:40,10*5,replace=TRUE),ncol=5)),letters[1:5])
indx<-as.data.frame(combn(names(dat1),2),stringsAsFactors=FALSE)
res<-t(sapply(indx,function(x) {x1<-cbind(dat1[x[1]],dat1[x[2]]);summary(lm(x1[,1]~x1[,2]))$coef[,4]}))
rownames(res)<-apply(indx,2,paste,collapse="_")
colnames(res)[2]<- "Coef1"
head(res,3)
# (Intercept) Coef1
#a_b 0.39862676 0.8365606
#a_c 0.02427885 0.6094141
#a_d 0.37521423 0.7578723
#permutation
indx2<-expand.grid(names(dat1),names(dat1),stringsAsFactors=FALSE)
#or
indx2<- expand.grid(rep(list(names(dat1)),2),stringsAsFactors=FALSE)
indx2New<- indx2[indx2[,1]!=indx2[,2],]
res2<-t(sapply(seq_len(nrow(indx2New)),function(i) {x1<- indx2New[i,]; x2<-cbind(dat1[x1[,1]],dat1[x1[,2]]);summary(lm(x2[,1]~x2[,2]))$coef[,4]}))
row.names(res2)<-apply(indx2New,1,paste,collapse="_")
colnames(res2)<- colnames(res)
A.K.
Hi everyone,
First off just like to say thanks to everyone´s contributions.
Up until now, I´ve never had to post as I´ve always found the answers
from trawling through the database. I´ve finally managed to stump
myself, and although for someone out there, I´m sure the answer to my
problem is fairly simple, I, however have spent the whole day infront of
my computer struggling. I know I´ll probably get an absolute ribbing
for making a basic mistake, or not understanding something fully, but
I´m blind to the mistake now after looking so long at it.
What I´m looking to do, is formulate a matrix ([28,28]) of
p-values produced from running linear regressions of 28 variables
against themselves (eg a~b, a~c, a~d.....b~a, b~c etc...), if that makes
sense. I´ve managed to get this to work if I just input each variable
by hand, but this isn´t going to help when I have to make 20 matrices.
My script is as follows;
for (j in [1:28])
{
##This section works perfectly, if I don´t try to loop it, I know
this wont work at the moment, because I haven´t designated what j is,
but I´m showing to highlight what I´m attempting to do.
models <- lapply(varlist, function(x) {
lm(substitute(ANS ~ i, list(i = as.name(x))), data = con.i)
})
abc<- lapply(models, function(f) summary(f)$coefficients[,4])
abc<- do.call(rbind, abc)
}
I get the following error when I try to loop it...
Error in model.frame.default(formula = substitute(j ~ i, list(i = as.name(x))), :
variable lengths differ (found for 'ANS') ##ÄNS being my first variable
All variables are of the same length, with 21 recordings for each
If anyone can suggest a method of looping, or another means
or producing ´models´ for each of my 28 variables, without having to do
it by hand that would be fantastic.
Thanks in advance!!
More information about the R-help
mailing list