R looping with bp function -
i have 1 dataset containing 3 columns: country, year , tdvalue. make loop country create dummy variable (sd
) having 1 or 0 if year breakpoint using r breakpoint
function. when make code work sd
variable equal 0, while know case years?
thanks lot help!
library(zoo) library(sandwich) library(strucchange) library(segmented) library(tree) tabo<-read.table("boucle.txt", header=t, sep="\t") fonction.bp<-function(b) bp.inf <- breakpoints(tabo$year ~ tabo$tradevaluein1000usd , tabo = tabo[b,], h = 8) t<-breakdates(confint(bp.inf)) (i in 1:nrow(t)) { res <- ifelse(tabo$year[b] == t[i,1] , 1, 0) return(res) } } numero<-1:nrow(tabo) tabo$sd<-lapply(tabo$code_o,fonction.bp)
data sample:
code_o -origin -year -tradevaluein1000usd abw aruba 1988 375.059 abw aruba 1989 3458.656 abw aruba 1990 2924.484 abw aruba 1991 140509.4
etc several countries
dput(tabo):
structure(list(code_o = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l), .label = c("abw", "afg", "ago"), class = "factor"), origin = structure(c(3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l), .label = c("afghanistan", "angola", "aruba"), class = "factor"), year = c(1988l, 1989l, 1990l, 1991l, 1992l, 1993l, 1994l, 1995l, 1996l, 1997l, 1998l, 1999l, 2000l, 2001l, 2002l, 2003l, 2004l, 2005l, 2006l, 2007l, 2008l, 2009l, 2010l, 2011l, 2012l, 1988l, 1989l, 1990l, 1991l, 1992l, 1993l, 1994l, 1995l, 1996l, 1997l, 1998l, 1999l, 2000l, 2001l, 2002l, 2003l, 2004l, 2005l, 2006l, 2007l, 2008l, 2009l, 2010l, 2011l, 2012l, 1988l, 1989l, 1990l, 1991l, 1992l, 1993l, 1994l, 1995l, 1996l, 1997l, 1998l, 1999l, 2000l, 2001l, 2002l, 2003l, 2004l, 2005l, 2006l, 2007l, 2008l, 2009l, 2010l, 2011l, 2012l ), tradevaluein1000usd = c(375.059, 3458.656, 2924.484, 140509.4, 326377, 548739.3, 570287.9, 673563.2, 809647.7, 1021996, 680243.7, 944974.8, 1950097, 1416807, 1055372, 1276015, 2503752, 3908081, 4294362, 4654180, 5523432, 2203173, 272596.5, 4450387, 127760.6, 121861.2, 125059.8, 134163.4, 115283.5, 82499.51, 68673.89, 97143.18, 104883.2, 124654.5, 155892.9, 167802.9, 137721, 153405.3, 99146.39, 103894.9, 190640.9, 209073.9, 264083.6, 254765.3, 408123.6, 507407, 1283451, 609946.1, 486418.4, 67638.02, 1112926, 3120863, 4082248, 3290223, 3796494, 3283747, 3175830, 3614761, 4669298, 4618304, 3501481, 4478671, 7878114, 6290144, 7344164, 8563406, 11900000, 20700000, 30200000, 39500000, 65700000, 38900000, 50600000, 59400000, 8839811 ), sd = list(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .names = c("code_o", "origin", "year", "tradevaluein1000usd", "sd"), row.names = c(na, -75l), class = "data.frame")
you've got bad code.
if expect people's time, must put more effort in.
your function doesn't work: tabo['ago',]
, tabo['afg',]
, , tabo['abw',]
empty, there no rows these names. think want subset data, using like:
tabo[tabo$code_o == 'ago',] tabo[tabo$code_o == 'afg',] tabo[tabo$code_o == 'abw',]
bp.inf
same regardless of if include code tabo = tabo[b,]
-- you're calling items the regression global environment, , not passing in data-frame (as you're giving tabo
rather data
). if confusing forget ...
the bottom line there few erros in line finds breakpoints. need change bp.inf <- breakpoints(year ~ tradevaluein1000usd, data = tabo[tabo$code_o == 'ago',], h = 8)
.
note also, function doesn't open {
doesn't work @ all.
Comments
Post a Comment