[R] R-code for carrying out step-wise panel regression
Dear all For one of my empirical research investigation, I tried to carry out step-wise panel regression analysis by making adaptation in the use of plm package (since stepAIC command does not work with plm). Say, I tried to regress an explained variable (DEP) on 3 explanatory variables (EX1, EX2, EX3) using a panel data set (dat). Of course, the required set of instructions would be:require(plm)reg <- plm(DEP ~ EX1 + EX2 + EX3, data = dat, method = "within") ... (A)But, what I wish to do (for the purpose of carrying out the analysis iteratively) is as follows:dep <- "DEP"exp <- c("EX1", "EX2", "EX3")and then use somehow these dep and exp to get a statement compatible with (A). I tried the following frm <- as.formula(paste(dep, "~", exp))reg <- plm(frm, data = dat, method = "within)But it did not work properly. I could get the output only in respect of DEP ~ EX1, whereas I need the same in respect of DEP ~ EX1 + EX2 + EX3.Kindly help. Or, you may pl. suggest some alternative via media.RegardsAmarjit Singh [[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.
[R] command for plm
Dear allI am trying to carry out step-wise panel regression analysis by making adaptation in the use of plm package. Say, I am trying to regress the explained variable (DEP) on 3 explanatory variables (EX1, EX2, EX3) using a panel data set (dat). Of course, the required set of instructions would be:require(plm)reg <- plm(DEP ~ EX1 + EX2 + EX3, data = dat, method = "within") ... (A)But, what I wish to do (for the purpose of carrying out the analysis iteratively) is as follows:dep <- "DEP"exp <- c("EX1", "EX2", "EX3")and then use somehow these dep and exp to get a statement compatible with (A). I tried the following frm <- as.formula(paste(dep, "~", exp)) reg <- plm(frm, data = dat, method = "within)But it did not work properly. I could get the output only in respect of DEP ~ EX1, whereas I need the same in respect of DEP ~ EX1 + EX2 + EX3.Kindly help. RegardsAmarjit Singh [[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.
[R] How to define a number of matrices through loops
Hi all I need to deal with a number (say, 5) of diferent ordered matrices simultaneously in my computational work. I tried to define these matrices through looping, but got an error message: row <- c(3,4,6,4,5) mt <- c() for (i in 1:5){ mt[i] <- matrix(nrow=row[i],ncol=4) } mt Kindly help Yours ajss __ 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.
Re: [R] Repetitive correlation test
Dear Burns Replacing comma through tilde has not helped; I got the message: Error in cor.test.formula(frm, alternative = "two.sided", method = "pearson") : 'formula' missing or invalid. As you know, the syntax for test for corelation coefficient b/w variables x1 & x2 is as: cor=cor.test(x1,x2,alternative = "two.sided", method = "pearson") That is, the variables essentially need be separated by a comma. So please help. --- On Sat, 25/10/08, Patrick Burns <[EMAIL PROTECTED]> wrote: > From: Patrick Burns <[EMAIL PROTECTED]> > Subject: Re: [R] Repetitive correlation test > To: [EMAIL PROTECTED] > Date: Saturday, 25 October, 2008, 2:39 PM > I think you mean to paste a tilde rather than a > comma in the command that creates 'frm'. > > Patrick Burns > [EMAIL PROTECTED] > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of S Poetry and "A Guide for the Unwilling S > User") > > Amarjit Singh Sethi wrote: > > Dear all, > > Through the following code, I wanted to perform > correlation test repetedly (through loop) on different > combinations of variables of a data set. > > > > Code: > > > > > x=read.table("sample.txt",header=T,sep="\t") > > out="corout.txt" > > sink(out) > > nm = names(x) > > print(nm) > > nvr=3 > > # nvr=Total no. of variables in the input data file > > for (i in 1:(nvr-1)) > > { > > for(j in (i+1):nvr) > > { > > frm= > as.formula(paste(nm[i+1],",",nm[j+1],sep="")) >($$) > > > crl=cor.test(frm,alternative="two.sided",method="pearson") > > > smr=summary(crl) > > print(smr) > > } > > } > > sink() > > > > Sample Data: > > > > S NoV1 V2 V3 > > 1 15 10 4 > > 2 6 4 7 > > 3 10 5 2 > > 4 8 6 6 > > > > But the code does not work; there seems to be some > problem in the statement marked ($$) above. The message I > get is: > > > > Error in parse(text = x) : unexpected ',' in > "V1," > > > > Kindly help to get rid of the bug > > > > Regards > > > > ajss > > > > > > Unlimited freedom, unlimited storage. Get it > now, on > http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/ > > > > __ > > 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. > > > > > > Download prohibited? No problem. CHAT from any browser, without download. Go to http://in.webmessenger.yahoo.com/ __ 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.
[R] Repetitive correlation test
Dear all, Through the following code, I wanted to perform correlation test repetedly (through loop) on different combinations of variables of a data set. Code: x=read.table("sample.txt",header=T,sep="\t") out="corout.txt" sink(out) nm = names(x) print(nm) nvr=3 # nvr=Total no. of variables in the input data file for (i in 1:(nvr-1)) { for(j in (i+1):nvr) { frm= as.formula(paste(nm[i+1],",",nm[j+1],sep=""))($$) crl=cor.test(frm,alternative="two.sided",method="pearson") smr=summary(crl) print(smr) } } sink() Sample Data: S NoV1 V2 V3 1 15 10 4 2 6 4 7 3 10 5 2 4 8 6 6 But the code does not work; there seems to be some problem in the statement marked ($$) above. The message I get is: Error in parse(text = x) : unexpected ',' in "V1," Kindly help to get rid of the bug Regards ajss Unlimited freedom, unlimited storage. Get it now, on http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/ __ 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.
Re: [R] Getting names of variables without quotes
Dear Menne/ Jorge Many thanks for the help rendered by you both. Now, may I augment my problem. While running the regressions (through 'for' loop), I tried to generate time series plots for each iteration. Although the summary results from regressions were saved in the output file (through sink() statement), yet the plots were not saved. The tried code was: x=read.table("sample.txt",header=T,sep="\t") out="output.txt" sink(out) x nm = names(x) nm for (i in 1:2) { for(j in (i+1):3) { formula= as.formula(paste(nm[i]," ~ ",nm[j],sep="")) slr=lm(formula, data=x) smr=summary(slr) print(smr) fit=fitted(slr) tsp=ts.plot(fit) } } sink() Kindly help to get the plots saved, preferably in separate output files for each iteration. regards ajss --- On Sun, 19/10/08, Dieter Menne <[EMAIL PROTECTED]> wrote: > From: Dieter Menne <[EMAIL PROTECTED]> > Subject: Re: [R] Getting names of variables without quotes > To: [EMAIL PROTECTED] > Date: Sunday, 19 October, 2008, 9:29 PM > Amarjit Singh Sethi yahoo.co.in> > writes: > > (Please do not use tabs when sending a data sample, these > must be > manually edited) > > V1,V2,V3 > 15,10,4 > 6,4,7 > 10,5,2 > 8,6,6 > > This does the work. However, I fear that you are going to > use it on > 100 variables, and I would strongly advice againts > searching for > linear relations > that way. > > Dieter > > x=read.table("sample.txt",header=T,sep=",") > nm = names(x) > for (i in 1:2) > { > for(j in (i+1):3) # note that you need the ()! > { > formula= > as.formula(paste(nm[i],"~",nm[j],sep="")) > slr=lm(formula, data=x) > smr=summary(slr) > print(smr) > } > } > > __ > 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 __ 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.
Re: [R] Getting names of variables without quotes
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" 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. Ki
[R] Getting names of variables without quotes
 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.
[R] Iterative estimation of linear regression model
Dear all I am intrested in making iterative estimation (thro' loop statements) of, say, linear regression model. For this purpose, I have written the following programme and that I have made use of a sample data (viz., exp.txt): Programme: # Linear regression modelling with sample data (try5.txt) # Repeated estimation through loop statement x=read.table("try5.txt",header=T,sep="\t") nm=names(x) out="slrout.txt" sink(out) nvr=10;nv1=4;nv2=nvr-nv1 for(i in 1:nv1){ dep=nm[i+1] # The 1st dependent variable is C1 (at column no. 2) for(j in 1:nv2){ ind=nm[j+nv1+1] # The first independent variable is C5 (at column no. 6) # Estimation of simple linear regression equation slr=lm(dep ~ ind,data=x) smr=summary(slr) print(slr) tsp=ts.plot(dep) tsp } } Data file (try.txt): Year C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 Y1 3.5 13.8 9.5 6.8 0.4 24.0 3 7.1 24.6 0.03 Y2 3.8 13.9 9.9 7.6 0.7 12.8 5 9.0 32.5 0.01 Y3 4.5 14.5 14.2 9.2 0.6 14.5 1 7.5 22.9 0.05 Y4 5.9 16.2 24.6 12.7 0.2 24.3 3 8.0 26.8 0.04 Y5 7.2 20.4 40.6 18.2 0.8 28.2 5 9.2 44.3 0.02 Y6 5.9 18.6 37.4 14.5 0.3 36.9 8 9.5 32.9 0.10 Y7 8.0 16.1 88.6 24.1 0.1 34.6 2 8.7 11.1 0.02 Y8 13.6 21.1 56.3 19.0 0.7 33.3 6 9..5 10.8 0.06 Y9 11.2 20.4 40.7 12.9 1.1 40.3 3 12.2 6.5 0.04 Y10 7.6 18.3 27.5 8.1 2.3 41.9 2 5.9 2.9 1.00 Y11 8.8 22.2 33.3 8.8 0.6 44.4 4 6.6 55.5 0.09 Y12 9.4 16.5 35.6 16.2 0.7 50.2 5 8..8 31.4 0.07 (These files have been given as attachments as well) The data file has 10 variables in all, divided into two groups: G1 consisting of the first 4 variables and G2 of the remaining 6 variables. We wish to iteratively pick one variable from the group G1 and regress it upon (an iteratively picked) variable from G2 and save the summary results. We also wish to save the repeatedly generated diagrams in an output file. But the programme has failed to work. Kindly help; I'm a raw hand in R-language. Regards ajss Be the first one to try the new Messenger 9 Beta! Go to http://in.messenger.yahoo.com/win/# Linear regression modelling with sample data (try5.txt) # Repeated estimation through loop statement x=read.table("try5.txt",header=T,sep="\t") nm=names(x) out="slrout.txt" sink(out) nvr=10;nv1=4;nv2=nvr-nv1 for(i in 1:nv1){ dep=nm[i+1] # The 1st dependent variable is C1 (at column no. 2) for(j in 1:nv2){ ind=nm[j+nv1+1] # The first independent variable is C5 (at column no. 6) # Estimation of simple linear regression equation slr=lm(dep ~ ind,data=x) smr=summary(slr) print(slr) tsp=ts.plot(dep) tsp } } YearC1 C2 C3 C4 C5 C6 C7 C8 C9 C10 Y1 3.5 13.89.5 6.8 0.4 24.03 7.1 24.6 0.03 Y2 3.8 13.99.9 7.6 0.7 12.85 9.0 32.5 0.01 Y3 4.5 14.514.29.2 0.6 14.51 7.5 22.9 0.05 Y4 5.9 16.224.612.70.2 24.33 8.0 26.8 0.04 Y5 7.2 20.440.618.20.8 28.25 9.2 44.3 0.02 Y6 5.9 18.637.414.50.3 36.98 9.5 32.9 0.10 Y7 8.0 16.188.624.10.1 34.62 8.7 11.1 0.02 Y8 13.621.156.319.00.7 33.36 9.5 10.8 0.06 Y9 11.220.440.712.91.1 40.33 12.26.5 0.04 Y10 7.6 18.327.58.1 2.3 41.92 5.9 2.9 1.00 Y11 8.8 22.233.38.8 0.6 44.44 6.6 55.5 0.09 Y12 9.4 16.535.616.20.7 50.25 8.8 31.4 0.07__ 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.
[R] Suitable package for carrying out sigma and beta convergence in panel data
Dear all I wish to carry out sigma- and beta-convergence analysis in respect of panel data [wherein current value of one of the variables needs be regressed upon suitably transformed lagged values of another variable(s)]. I am quite new to the R-language and am not very much aware of the availbaility of suitable package(s)/ code in the language. Can any one help me in letting me know of appropriate package/procedural steps to undertake the anlaysis. Kindly let me know as well, format of the input data file for such a package. Regards Amarjit Singh Sethi Bollywood, fun, friendship, sports and more. You name it, we have it on http://in.promos.yahoo.com/groups/bestofyahoo/ [[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.
[R] Importing data in text file into R
Dear all, I am quite new to R; facing certain problems: Say, I have a text file( named as "try"): YearC1 C2 C3 C4 C5 C6 Y1 3.5 13.89.5 6.8 0.4 24.2 Y2 3.8 13.99.9 7.6 0.7 12.8 Y3 4.5 14.514.29.2 0.6 14.5 Y4 5.9 16.224.612.70.2 24.3 Y5 7.2 20.440.618.20.8 28.2 Y6 5.9 18.637.414.50.3 36.9 Y7 8.0 16.188.624.10.1 34.6 Y8 13.621.156.319.00.7 33.3 I wish to import the file into R and make certain computations, like intercorrelation matrix. I tried the following syntax: # Inputting the data file (saved in text format) df="trial.txt" idt=read.table(df,header=T, sep="\t") idt # To generate intercorrelatio matrix r = cor(idt) r=round(r, 4) r The seems to have been read, but further computations not made, with the following output: > # Inputting the data file (saved in text format) > df="trial.txt" > idt=read.table(df,header=T, sep="\t") > idt Year C1 C2 C3 C4 C5 C6 1 Y1 3.5 13.8 9.5 6.8 0.4 24.2 2 Y2 3.8 13.9 9.9 7.6 0.7 12.8 3 Y3 4.5 14.5 14.2 9.2 0.6 14.5 4 Y4 5.9 16.2 24.6 12.7 0.2 24.3 5 Y5 7.2 20.4 40.6 18.2 0.8 28.2 6 Y6 5.9 18.6 37.4 14.5 0.3 36.9 7 Y7 8.0 16.1 88.6 24.1 0.1 34.6 8 Y8 13.6 21.1 56.3 19.0 0.7 33.3 > # To generate intercorrelatio matrix > r = cor(idt) Error in cor(idt) : missing observations in cov/cor In addition: Warning message: In cor(idt) : NAs introduced by coercion > r=round(r, 4) Error: object "r" not found > r Error: object "r" not found Kindly advise me as to how to get rid of the error message. AJSS __ 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.