非常感谢您的及时回复,非常感谢!我在这里附上整个代码和一些简短的 cmets:
for (i in 2005:2007){
ntm_data <-subset(ntm_data_wip, StartDate <=i & EndDate >i)
# Once the data is loaded, I exclude NTM codes which are missing.
# I only need the reporter, NTM code and product codes (HS 6-digit codes).
ntm_data <- ntm_data[!is.na(ntm_data$ntmcode)&ntm_data$ntmcode!="",]
ntm_data <- ntm_data[,c("reporter", "ntmcode", "hs6")]
# I group the data by reporter, NTM and product code (hs6) and count the number of combinations in a new variable called count.
ntm_data <- ntm_data %>% group_by(reporter, ntmcode, hs6) %>%
summarise(count = n())
head(ntm_data)
# I prepare the regulatory matrix by creating a list of countries for which I want the regulatory distance. The
# regulatory matrix shows the distance between two countries and has as column and row names the ISO3 codes of the countries.
# As specified above, I am interested in having the analysis for all available countries.
avail_iso3s <- unique(ntm_data$reporter)
# I create an empty regulatory distance matrix. For column size I use the length of avail_iso3s and add 1 for the reporter column.
# I populate the column names with reporter and the ISO3 codes with the option dimnames.
regulatory_distance_matrix <- data.frame(matrix(vector(),0,length(avail_iso3s)+1,
dimnames = list(c(), c("reporter", avail_iso3s )
)),
stringsAsFactors=F)
#' Now I can move on to calculating the regulatory distance formula in page 3 of "DEEP REGIONAL INTEGRATION AND NON-TARIFF MEASURES:A METHODOLOGY FOR DATA ANALYSIS (2015)" .
#' As N is a constant, I start with calculating it outside of the loop
N <- ntm_data %>% group_by(ntmcode, hs6) %>% count()
N <- nrow(N)
# I now fill in the regulatory distance matrix with values
for (g in 1:length(avail_iso3s)){
country_a <- ntm_data[ntm_data$reporter==avail_iso3s[g],c("ntmcode", "hs6")]
country_a$country_a <- 1
regulatory_distance_matrix[g,"reporter"] <- avail_iso3s[g]
for (k in 1:length(avail_iso3s)){
if (!is.na(regulatory_distance_matrix[k,avail_iso3s[g]])){next }
country_b <- ntm_data[ntm_data$reporter==avail_iso3s[k],c("ntmcode", "hs6")]
country_b$country_b <- 1
merged <- merge(country_a, country_b, by=c("ntmcode", "hs6"), all = TRUE)
merged[is.na(merged)] <- 0
merged$abs_diff <- abs(merged$country_a-merged$country_b)
rd <- sum(merged$abs_diff)/N
regulatory_distance_matrix[g,avail_iso3s[k]] <- rd
}
}
# Now I fill in the missing values and create a Stata dta.file.
for (g in 1:length(avail_iso3s)){
for (k in 1:length(avail_iso3s)){
if (is.na(regulatory_distance_matrix[k,avail_iso3s[g]])){
regulatory_distance_matrix[k,avail_iso3s[g]] <- regulatory_distance_matrix[g,avail_iso3s[k]]
}
}
}
regulatory_distance_matrix$year <-i
write.dta(regulatory_distance_matrix, "C:/Users/Utente/Desktop/Master's thesis/Thesis analysis/- RD construction/Binary RD/new_regulatory_distance_matrix_",i,".dta")
}
我希望这会很有用。另外,在遵循您的建议后,我收到以下错误:
`summarise()` has grouped output by 'reporter', 'ntmcode'. You can override using the `.groups` argument.
Error in if (convert.dates) { :
l'argomento non può essere interpretato come logico
Inoltre: Warning message:
In write.dta(regulatory_distance_matrix, "C:/Users/Utente/Desktop/Master's thesis/Thesis analysis/- RD construction/Binary RD/new_regulatory_distance_matrix_", :
Version must be 6-12: using 7
对于这个繁琐的问题,我深表歉意,但我对 R 很陌生,其他帖子似乎没有多大帮助。