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
}
}
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
______________________________________________
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.