[R] Looping an lapply linear regression function

arun smartpink111 at yahoo.com
Tue Sep 10 17:31:47 CEST 2013


Hi,
Try:

dat2<- read.csv("BOlValues.csv",header=TRUE,sep="\t",row.names=1)
dim(dat2)
#[1] 20 28

indx2<-expand.grid(names(dat2),names(dat2),stringsAsFactors=FALSE)
nrow(indx2)
#[1] 784

indx2New<- indx2[indx2[,1]!=indx2[,2],]
nrow(indx2New)
#[1] 756

res2<-sapply(seq_len(nrow(indx2New)),function(i) {x1<- indx2New[i,]; x2<-cbind(dat2[x1[,1]],dat2[x1[,2]]);summary(lm(x2[,1]~x2[,2]))$coef[,4][2]}) #changed here
dat3<- cbind(indx2New,value=res2)
dim(dat3)
#[1] 756   3
library(reshape2)
res2New<- dcast(dat3,Var1~Var2,value.var="value")
row.names(res2New)<- res2New[,1]
 res2New<- as.matrix(res2New[,-1])
 dim(res2New)
#[1] 28 28
A.K.


________________________________
From: Rhys Manners <rhys.manners at hotmail.co.uk>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Tuesday, September 10, 2013 11:10 AM
Subject: RE: Looping an lapply linear regression function




Hi,

Thanks for responding to my post, data set attached...

R


> Date: Tue, 10 Sep 2013 08:04:30 -0700
> From: smartpink111 at yahoo.com
> To: rhys.manners at hotmail.co.uk
> Subject: Re: Looping an lapply linear regression function
> 
> Hi,
> Any chance you could email me the dataset you tested for? I will take a look at it.
> Tx.
> 
> 
> 
> <quote author='Rhys_Man'>
> Arun,
> 
> Any reason why I´d keep getting this error whenever I try and run this code
> on fferent country data, which is identical in terms of structure as the
> example I sent you, except for the data being obviously different.
> 
> Error in structure(ordered, dim = ns) : 
>   dims [product 784] do not match the length of object [0]
> 
> 
> 
> </quote>
> Quoted from: 
> http://r.789695.n4.nabble.com/Looping-an-lapply-linear-regression-function-tp4675475p4675761.html
> 
> 
> _____________________________________
> Sent from http://r.789695.n4.nabble.com
>



More information about the R-help mailing list