我有一個數據集包含3列:country,year和tdvalue。 我想通過使用R breakpoint
函數按國家創建一個循環來創建一個具有1或0的虛擬變量(sd
),如果年份是斷點。 但是,當我讓我的代碼工作時,我的sd
變量總是等於0,而我知道這是幾年的情況?R與bp功能循環
非常感謝您的幫助!
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))
for (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)
數據樣本:
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
等幾個國家
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")
請輸入您的數據,或將我們指向源代碼,以便您的問題很容易重現。 – ricardo
謝謝,請看現在 – aleeyah
任何幫助pleaseee – aleeyah