Dear R experts,
Might be very simple question to ask but would be insightful. As the same story
of nested "for loops". following is the code that I am using to get
the autocorrelation function of the sample data. I have tried to get rid of for
loops but since I am touching R after such a long time that I need to practice
more but I need help to revive my skills. I know that apply() or sapply() would
be beneficial. I need help...
Best Wishes,
Baloo
R code
----------
acf_wal <- function (infile) {
data<-read.table(infile)
data_value <- data[,-1]
data_value_mean <- mean(data_value)
data_value_square <- (data_value - data_value_mean) ^ 2
square_sum<-sum(data_value_square)
entry<-NROW(data_value)
deno<-square_sum/entry
tab1<-c()
tab2<-c()
ps_value <- seq(0,(floor(entry/2)),1)
for(k in 0:(floor(entry/2))){
for (i in 1:(entry-k)) {
mult<-(data_value[i] - data_value_mean) * (data_value [i+k] -
data_value_mean)
tab1 <- c(tab1,mult)
}
auto_avg<-mean(tab1)
tab1<-c()
auto_corr<-auto_avg/deno
tab2<-c(tab2,auto_corr)
}
table_value <- cbind (ps_value, tab2)
colnames(table_value) <- c("#ps", "acf")
outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]
write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}
--------------
Sample data
------------------
1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079
--------------------
Help in this regard would be highly appreciated.
[[alternative HTML version deleted]]
Baloo,
Why don't you use the built-in acf function?
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than
asking him to perform a post-mortem examination: he may be able to say what the
experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens baloo mia
Verzonden: dinsdag 22 december 2009 2:07
Aan: r-help at r-project.org
Onderwerp: [R] Nested For loops
Dear R experts,
Might be very simple question to ask but would be insightful. As the same story
of nested "for loops". following is the code that I am using to get
the autocorrelation function of the sample data. I have tried to get rid of for
loops but since I am touching R after such a long time that I need to practice
more but I need help to revive my skills. I know that apply() or sapply() would
be beneficial. I need help...
Best Wishes,
Baloo
R code
----------
acf_wal <- function (infile) {
??? data<-read.table(infile)
??? data_value <- data[,-1]
??? data_value_mean <- mean(data_value)
??? data_value_square <- (data_value - data_value_mean) ^ 2
??? square_sum<-sum(data_value_square)
??? entry<-NROW(data_value)
??? deno<-square_sum/entry
??? tab1<-c()
??? tab2<-c()
??? ps_value <- seq(0,(floor(entry/2)),1)
???
??? for(k in 0:(floor(entry/2))){
??? ?? ?for (i in 1:(entry-k)) {
??? ?? ??? ?mult<-(data_value[i] - data_value_mean) * (data_value [i+k] -
data_value_mean)
??? ?? ??? ?tab1 <- c(tab1,mult)
??? ?? ?}
??? ?? ??? ?auto_avg<-mean(tab1)
??? ?? ??? ?tab1<-c()
??? ?? ??? ?auto_corr<-auto_avg/deno
??? ?? ??? ?tab2<-c(tab2,auto_corr)
??? }
??? table_value <- cbind (ps_value, tab2)
??? colnames(table_value) <- c("#ps", "acf")
??? outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]
???
write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}
--------------
Sample data
------------------
1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079
--------------------
Help in this regard would be highly appreciated.
?
[[alternative HTML version deleted]]
Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
In the ACF(nlme) the normalization of the numerator has been done by N and I
want to normalize it by N-k, where N is the observations and k is the lag.
Baloo
--- On Tue, 12/22/09, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be>
wrote:
From: ONKELINX, Thierry <Thierry.ONKELINX at inbo.be>
Subject: RE: [R] Nested For loops
To: "baloo mia" <baloo_mia at yahoo.com>, r-help at
r-project.org
Date: Tuesday, December 22, 2009, 1:00 AM
Baloo,
Why don't you use the built-in acf function?
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than
asking him to perform a post-mortem examination: he may be able to say what the
experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens baloo mia
Verzonden: dinsdag 22 december 2009 2:07
Aan: r-help at r-project.org
Onderwerp: [R] Nested For loops
Dear R experts,
Might be very simple question to ask but would be insightful. As the same story
of nested "for loops". following is the code that I am using to get
the autocorrelation function of the sample data. I have tried to get rid of for
loops but since I am touching R after such a long time that I need to practice
more but I need help to revive my skills. I know that apply() or sapply() would
be beneficial. I need help...
Best Wishes,
Baloo
R code
----------
acf_wal <- function (infile) {
??? data<-read.table(infile)
??? data_value <- data[,-1]
??? data_value_mean <- mean(data_value)
??? data_value_square <- (data_value - data_value_mean) ^ 2
??? square_sum<-sum(data_value_square)
??? entry<-NROW(data_value)
??? deno<-square_sum/entry
??? tab1<-c()
??? tab2<-c()
??? ps_value <- seq(0,(floor(entry/2)),1)
???
??? for(k in 0:(floor(entry/2))){
??? ?? ?for (i in 1:(entry-k)) {
??? ?? ??? ?mult<-(data_value[i] - data_value_mean) * (data_value [i+k] -
data_value_mean)
??? ?? ??? ?tab1 <- c(tab1,mult)
??? ?? ?}
??? ?? ??? ?auto_avg<-mean(tab1)
??? ?? ??? ?tab1<-c()
??? ?? ??? ?auto_corr<-auto_avg/deno
??? ?? ??? ?tab2<-c(tab2,auto_corr)
??? }
??? table_value <- cbind (ps_value, tab2)
??? colnames(table_value) <- c("#ps", "acf")
??? outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]
???
write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}
--------------
Sample data
------------------
1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079
--------------------
Help in this regard would be highly appreciated.
?
? ? ?
??? [[alternative HTML version deleted]]
Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in? this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.