Re: [R] Iterative regression through a series
Thank you , Rui--it works very well now. I'll have to check out the attributes and classes of the things you changed to understand what was going wrong before. Integer versus numeric, perhaps. I'll look into seq_len() as well. Many, many thanks for your help. It will make this and several future projects much more streamlined. Regards, Mendi De : Rui Barradas ruipbarra...@sapo.pt Cc : r-help@r-project.org r-help@r-project.org Envoyé le : Mercredi 3 avril 2013 3h43 Objet : Re: [R] Iterative regression through a series Hello, I've made a samll change to the code, and with your example data it's now working without errors. N - nrow(example) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in seq_len(N)) { regr - lm(example$Price[1:i] ~ example$Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas That is very helpful--I've run your code, and it works perfectly with the example data. However, I'm having some problems using my actual data--probably because my Time variable isn't actually a regular series (as it was in the example: Time=1:100). When run, it's producing estim and error vectors of lengths much greater than N. Here's a subset of my actual data: dput(example) structure(list(Time = c(3075L, 3168L, 3318L, 3410L, 3534L, 3715L, 3776L, 3926L, 3987L, 4110L, 4232L, 4291L, 4413L, 4505L, 4536L, 4656L, 4782L, 4886L, 5018L, 5138L, 5187L, 5253L, 5384L, 5540L, 5669L, 5740L, 5796L, 5887L, 5963L, 6042L, 6126L, 6197L, 6280L, 6405L, 6464L, 6553L, 6659L, 6755L, 6847L, 6917L, 7001L, 7120L, 7216L), Price = c(2.08, 3.55, 5.75, 5.69, 4.47, 5.11, 2.74, 3.04, 3.87, 4.7, 6.61, 3.95, 4.63, 7.11, 3.08, 4.476628726, 7.472854559, 8.775893276, 6.34, 5.79, 3.98889, 4.01917, 3.69, 4.603636364, 5.242094366, 6.854871699, 5.163700257, 9.154206814, 8.712059541, 10.60635248, 10.58180221, 10.55396909, 10.67812007, 9.985298266, 10.57385693, 9.644945417, 11.62, 12.615, 13.61, 10.83, 8.38, 12.7, 8.94)), .Names = c(Time, Price), row.names = c(NA, -43L), class = data.frame) Is this as simple as replacing the expression: for (i in Time) { with for (i in 1:length(Time)) { or somesuch? Mendi De : Rui Barradas ruipbarra...@sapo.pt Cc : r-help@r-project.org r-help@r-project.org Envoyé le : Mardi 2 avril 2013 11h51 Objet : Re: [R] Iterative regression through a series Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0 #I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time
Re: [R] Iterative regression through a series
Hello, I've made a samll change to the code, and with your example data it's now working without errors. N - nrow(example) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in seq_len(N)) { regr - lm(example$Price[1:i] ~ example$Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Em 03-04-2013 01:57, triskell4-umbre...@yahoo.fr escreveu: That is very helpful--I've run your code, and it works perfectly with the example data. However, I'm having some problems using my actual data--probably because my Time variable isn't actually a regular series (as it was in the example: Time=1:100). When run, it's producing estim and error vectors of lengths much greater than N. Here's a subset of my actual data: dput(example) structure(list(Time = c(3075L, 3168L, 3318L, 3410L, 3534L, 3715L, 3776L, 3926L, 3987L, 4110L, 4232L, 4291L, 4413L, 4505L, 4536L, 4656L, 4782L, 4886L, 5018L, 5138L, 5187L, 5253L, 5384L, 5540L, 5669L, 5740L, 5796L, 5887L, 5963L, 6042L, 6126L, 6197L, 6280L, 6405L, 6464L, 6553L, 6659L, 6755L, 6847L, 6917L, 7001L, 7120L, 7216L), Price = c(2.08, 3.55, 5.75, 5.69, 4.47, 5.11, 2.74, 3.04, 3.87, 4.7, 6.61, 3.95, 4.63, 7.11, 3.08, 4.476628726, 7.472854559, 8.775893276, 6.34, 5.79, 3.98889, 4.01917, 3.69, 4.603636364, 5.242094366, 6.854871699, 5.163700257, 9.154206814, 8.712059541, 10.60635248, 10.58180221, 10.55396909, 10.67812007, 9.985298266, 10.57385693, 9.644945417, 11.62, 12.615, 13.61, 10.83, 8.38, 12.7, 8.94)), .Names = c(Time, Price), row.names = c(NA, -43L), class = data.frame) Is this as simple as replacing the expression: for (i in Time) { with for (i in 1:length(Time)) { or somesuch? Mendi De : Rui Barradas ruipbarra...@sapo.pt À : triskell4-umbre...@yahoo.fr triskell4-umbre...@yahoo.fr Cc : r-help@r-project.org r-help@r-project.org Envoyé le : Mardi 2 avril 2013 11h51 Objet : Re: [R] Iterative regression through a series Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Em 02-04-2013 17:44, triskell4-umbre...@yahoo.fr escreveu: Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0#I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { +regr=lm(Price[1:i]~Time[1:i]) +estim=c(estim,coef(summary(regr))[2,1]) +error=c(error,coef(summary(regr))[2,1]) +} Error: subscript out of bounds Many, many thanks in advance. Mendi [[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
[R] Iterative regression through a series
Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0 #I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { + regr=lm(Price[1:i]~Time[1:i]) + estim=c(estim,coef(summary(regr))[2,1]) + error=c(error,coef(summary(regr))[2,1]) + } Error: subscript out of bounds Many, many thanks in advance. Mendi [[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.
Re: [R] Iterative regression through a series
Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Em 02-04-2013 17:44, triskell4-umbre...@yahoo.fr escreveu: Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0 #I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { + regr=lm(Price[1:i]~Time[1:i]) + estim=c(estim,coef(summary(regr))[2,1]) + error=c(error,coef(summary(regr))[2,1]) + } Error: subscript out of bounds Many, many thanks in advance. Mendi [[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-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] Iterative regression through a series
Also look at zoo's rollapply. MW On Apr 2, 2013, at 13:51, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Em 02-04-2013 17:44, triskell4-umbre...@yahoo.fr escreveu: Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0 #I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { + regr=lm(Price[1:i]~Time[1:i]) + estim=c(estim,coef(summary(regr))[2,1]) + error=c(error,coef(summary(regr))[2,1]) + } Error: subscript out of bounds Many, many thanks in advance. Mendi [[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-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-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] Iterative regression through a series
That is very helpful--I've run your code, and it works perfectly with the example data. However, I'm having some problems using my actual data--probably because my Time variable isn't actually a regular series (as it was in the example: Time=1:100). When run, it's producing estim and error vectors of lengths much greater than N. Here's a subset of my actual data: dput(example) structure(list(Time = c(3075L, 3168L, 3318L, 3410L, 3534L, 3715L, 3776L, 3926L, 3987L, 4110L, 4232L, 4291L, 4413L, 4505L, 4536L, 4656L, 4782L, 4886L, 5018L, 5138L, 5187L, 5253L, 5384L, 5540L, 5669L, 5740L, 5796L, 5887L, 5963L, 6042L, 6126L, 6197L, 6280L, 6405L, 6464L, 6553L, 6659L, 6755L, 6847L, 6917L, 7001L, 7120L, 7216L), Price = c(2.08, 3.55, 5.75, 5.69, 4.47, 5.11, 2.74, 3.04, 3.87, 4.7, 6.61, 3.95, 4.63, 7.11, 3.08, 4.476628726, 7.472854559, 8.775893276, 6.34, 5.79, 3.98889, 4.01917, 3.69, 4.603636364, 5.242094366, 6.854871699, 5.163700257, 9.154206814, 8.712059541, 10.60635248, 10.58180221, 10.55396909, 10.67812007, 9.985298266, 10.57385693, 9.644945417, 11.62, 12.615, 13.61, 10.83, 8.38, 12.7, 8.94)), .Names = c(Time, Price), row.names = c(NA, -43L), class = data.frame) Is this as simple as replacing the expression: for (i in Time) { with for (i in 1:length(Time)) { or somesuch? Mendi De : Rui Barradas ruipbarra...@sapo.pt Cc : r-help@r-project.org r-help@r-project.org Envoyé le : Mardi 2 avril 2013 11h51 Objet : Re: [R] Iterative regression through a series Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0 #I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { + regr=lm(Price[1:i]~Time[1:i]) + estim=c(estim,coef(summary(regr))[2,1]) + error=c(error,coef(summary(regr))[2,1]) + } Error: subscript out of bounds Many, many thanks in advance. Mendi [[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. [[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.
Re: [R] Iterative regression through a series
Thank you, Michael--it looks like a fantastic package. I'm fairly new to R, and not at all familiar with the zoo class, but it seems to have great potential. I'll need to spend some time sorting out how to use regressions in it, because the syntax used in the examples is alien to me. Mendi De : Michael Weylandt michael.weyla...@gmail.com À : Rui Barradas ruipbarra...@sapo.pt @r-project.org r-help@r-project.org Envoyé le : Mardi 2 avril 2013 11h58 Objet : Re: [R] Iterative regression through a series Also look at zoo's rollapply. MW On Apr 2, 2013, at 13:51, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0 #I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { + regr=lm(Price[1:i]~Time[1:i]) + estim=c(estim,coef(summary(regr))[2,1]) + error=c(error,coef(summary(regr))[2,1]) + } Error: subscript out of bounds Many, many thanks in advance. Mendi [[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-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. [[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.
Re: [R] Iterative regression through a series
Hi: rollapply is a fine solution but if you frame your problem as a state space model where the observation equation is the linear regression you're interested in, the state space framework provided by the DLM package will give you the output you want at each step t. the gory details are explained in the paper below but I only have a hard copy ( somewhere ? ) and it's 14.00 from JSTOR. http://www.jstor.org/discover/10.2307/2284643?uid=3739832uid=2uid=4uid=3739256sid=21101972960881 On Tue, Apr 2, 2013 at 8:58 PM, David Chertudi david_txert...@yahoo.frwrote: Thank you, Michael--it looks like a fantastic package. I'm fairly new to R, and not at all familiar with the zoo class, but it seems to have great potential. I'll need to spend some time sorting out how to use regressions in it, because the syntax used in the examples is alien to me. Mendi De : Michael Weylandt michael.weyla...@gmail.com À : Rui Barradas ruipbarra...@sapo.pt @r-project.org r-help@r-project.org Envoyé le : Mardi 2 avril 2013 11h58 Objet : Re: [R] Iterative regression through a series Also look at zoo's rollapply. MW On Apr 2, 2013, at 13:51, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, The error comes from NAs where you would expect coefficients. Try the following. set.seed(7511) # Make the example reproducible N - 100 Time -1:N Price - rnorm(N, 8, 2) estim - numeric(N) # It's better to allocate the results error - numeric(N) # vectors in advance for (i in Time) { regr - lm(Price[1:i] ~ Time[1:i]) estim[i] - coef(regr)[2] if(is.na(coef(regr)[2])) error[i] - NA else error[i] - summary(regr)$coefficients[2,2] } Hope this helps, Rui Barradas Hello, Some context: let's say I have a data series (let's call it PRICE, for simplicity), sample size N. I have a desire to regress that on TIME, and then to use the TIME and intercept coefficients to predict the price in the next period and to use the standard error to calculate a confidence interval. This is all very easy. However, what I need help for is to calculate a confidence interval for each point in time: imagining that at the end of the 10th period I have 10 data points, and wish to regress them on the 10 periods to create a confidence interval for the next 'predicted' price. And so on from TIME[10:100]. So the first regression would be of PRICE[1:10] on TIME[1:10], the second of PRICE[1:11] on TIME[1:11], the third of PRICE[1:11] on TIME[1:11], and so on to PRICE[1:N] and TIME[1:N]. I'd like to be able to vary the starting point (so it would need to be an argument in the function, in this case it would be 10). The ultimate output of the code would be to save the TIME coefficients and standard errors it generates to two vectors, say TIME.coef and TIME.SE. I'm not sure if lapply() can be bent to my will, or if a for loop would be too inefficient, or what. I'm not new to R, but I'm fairly new to this kind of programming. This is a bungled mess of a narrative, and I apologize. Please feel free to use TIME=1:100 and PRICE=rnorm(100,8,2). Here's an attempt, which has failed for reasons I can only imagine. Any help getting this to work would be greatly appreciated. Any help doing this without loops would be even better. Time=1:100 Price=rnorm(100,8,2) estim=0#I'm hoping this will be the Time coefficient error=0 #I'm hoping this will be the standard error of the Time coefficient for (i in Time) { +regr=lm(Price[1:i]~Time[1:i]) +estim=c(estim,coef(summary(regr))[2,1]) +error=c(error,coef(summary(regr))[2,1]) +} Error: subscript out of bounds Many, many thanks in advance. Mendi [[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-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. [[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. [[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