Dear AJSS, The problem is in how we read the data set. Here is a slight modification:
# ------------------- # Data set # ------------------- mydata=data.frame( V1=c(15,6,10,8), V2=c(10,4,5,6), V3=c(4,7,2,6)) # ------------------- # Regression models # ------------------- # Combinations library(forward) comb=t(fwd.combn(colnames(mydata),2)) # Summaries res=apply(comb,1,function(x){ y1=mydata[,x[1]] x1=mydata[,x[2]] m=lm(y1~x1) summary(m) } ) names(res)= apply(comb,1,paste,collapse="/",sep=" ") res # Output in an external file sink("E:/out.txt") res sink() Be aware you need the "forward" package. HTH, Jorge > sessionInfo() R version 2.7.2 Patched (2008-09-20 r46656) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] forward_1.0.1 MASS_7.2-44 On Sun, Oct 19, 2008 at 2:31 PM, Amarjit Singh Sethi <[EMAIL PROTECTED]>wrote: > Dear Jorge > > Many thanks for your prompt reply. I tried to make use of the code provided > by you (as per file attached), but it did not work for me; got the following > output: > > > # ------------------- > > # Data set > > # ------------------- > > mydata=read.table(textConnection(" > + V1V2V3 > + 115104 > + 2647 > + 31052 > + 4866"),header=TRUE) > > closeAllConnections() > > # ------------------- > > # Regression models > > # ------------------- > > # Combinations > > library(forward) > > comb=t(fwd.combn(colnames(mydata),2)) > Error in fwd.combn(colnames(mydata), 2) : n < m > > # Summariesres=apply(comb,1,function(x){ > > y1=mydata[,x[1]] > Error in .subset(x, j) : invalid subscript type 'list' > > x1=mydata[,x[2]] > Error in .subset(x, j) : invalid subscript type 'list' > > m=lm(y1~x1) > Error in eval(expr, envir, enclos) : object "y1" not found > > summary(m) > Error in summary(m) : object "m" not found > > } > Error: unexpected '}' in "}" > > ) > Error: unexpected ')' in ")" > > names(res)= apply(comb,1,paste,collapse="/",sep=" ") > Error in apply(comb, 1, paste, collapse = "/", sep = " ") : > object "comb" not found > > # Output in an external file > > sink("E:/out.txt") > > res > Error: object "res" not found > > sink() > > > > I do not know as to where the problem lies (Frankly, I have not been able > to grasp intricacies of this code). Will you kindly help me in getting rid > of the problem. > > Regards > ajss > > > --- On Sun, 19/10/08, Jorge Ivan Velez <[EMAIL PROTECTED]> wrote: > > From: Jorge Ivan Velez <[EMAIL PROTECTED]> > Subject: Re: [R] Getting names of variables without quotes > To: [EMAIL PROTECTED] > Cc: "Dieter Menne" <[EMAIL PROTECTED]>, "R mailing list" < > r-help@r-project.org> > Date: Sunday, 19 October, 2008, 9:19 PM > > > > > Dear AJSS, > > > Perhaps: > > > > # ------------------- > # Data set > # ------------------- > > > mydata=read.table(textConnection(" > V1 V2 V3 > 1 15 10 4 > 2 6 4 7 > 3 10 5 2 > 4 8 6 6"),header=TRUE) > closeAllConnections() > > > # ------------------- > # Regression models > # ------------------- > > > # Combinations > library(forward) > comb=t(fwd.combn(colnames(mydata),2)) > > > # Summaries > res=apply(comb,1,function(x){ > y1=mydata[,x[1]] > x1=mydata[,x[2]] > m=lm(y1~x1) > summary(m) > } > ) > names(res)= apply(comb,1,paste,collapse="/",sep="") > > > # Output in an external file > sink("C:/out.txt") > res > sink() > > > HTH, > > > Jorge > > > > > On Sun, Oct 19, 2008 at 11:38 AM, Amarjit Singh Sethi <[EMAIL PROTECTED]> > wrote: > > > > > > > Dear Jorge/Dieter > Thank you very much for your help. I indeed have been able to get names of > the variables through 'noquote( )'statement. Yet my problem (regarding > running regression analysis iteratively: of V1 upon V2; of V1 upon V3; and > of V2 upon V3) could not be solved. My input data: > > > V1 V2 V3 > 1 15 10 4 > 2 6 4 7 > 3 10 5 2 > 4 8 6 6 > > I tried the following code: > > x=read.table("sample.txt",header=T,sep="\t") > sink("out.txt") > for (i in 1:2){ > dep=noquote(nm[i]) > for(j in i+1:3){ > ind=noquote(nm[j]) > slr=lm(dep ~ ind, data=x) > smr=summary(slr) > smr > } > } > sink() > But I could not get any results. Kindly help or suggest suitable > alternative. > The data and the code are given as attachments also. > Regards > ajss > > > --- On Sun, 19/10/08, Jorge Ivan Velez <[EMAIL PROTECTED]> wrote: > > From: Jorge Ivan Velez <[EMAIL PROTECTED]> > Subject: Re: [R] Getting names of variables without quotes > To: [EMAIL PROTECTED] > Cc: r-help@r-project.org > Date: Sunday, 19 October, 2008, 1:04 AM > > > > > > > > > Dear AJSS, > > > Perhaps ?noquote might be useful for you. Here is an example: > > > > x=c('V1','V2','V3','V4') > > > > x > [1] "V1" "V2" "V3" "V4" > > noquote(x) > [1] V1 V2 V3 V4 > > HTH, > > > Jorge > > > > > > On Sat, Oct 18, 2008 at 3:04 PM, Amarjit Singh Sethi <[EMAIL PROTECTED]> > wrote: > >  > Dear R-helpers, > hello > I am seeking your help in somehow getting names of variables without > quotes (" "). > Let us say, we have a table with 3 variables V1, V2 and V3. After the > table is read, I get names of the variables (thro' the following code) as > under quotes (like "V1" rather than the original representation V1) >  >  x=read.table("sample.txt",header=T,sep="\t") > > x >    V1   V2   V3 > 1 15    10     4 > 2   6      4      7 > 3 10     5      2 > 4   8     6      6 > > nm=names(x) > > nm > [1] "V1" "V2" "V3" >  > In fact I need the variables in the original representation (i.e.., as > they appear in the input data file) so as to use them repeatedly (through > loop statement) in regression analysis. Kindly help. > Regards > ajss > > > > > d Now! http://messenger.yahoo.com/download.php > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > > > > Send free SMS to your Friends on Mobile from your Yahoo! Messenger. > Download Now! http://messenger.yahoo.com/download.php > > > Send free SMS to your Friends on Mobile from your Yahoo! Messenger. > Download Now! http://messenger.yahoo.com/download.php > [[alternative HTML version deleted]]
______________________________________________ R-help@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.