r 一个简单的R脚本,用于在Win / Open BUGS中创建在OpenBUGS中运行的邻接数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 一个简单的R脚本,用于在Win / Open BUGS中创建在OpenBUGS中运行的邻接数据相关的知识,希望对你有一定的参考价值。
#Thanh Le Viet
#Ver 0.2
#Logs: Added comma for each row of adj
#Date: Apr 20th 2016
rm(list=ls())
if (!"pacman" %in% installed.packages()[,1]) #Check to install pacman (package manager) if not installed on the computer
install.packages('pacman')
library(pacman) #Load the package manager
p_load(R2OpenBUGS, glmmBUGS, rgdal, spdep) #Using package manager to check and load the other needed packages. If not installed, pacman will download and install.
#R2OpenBUGS will be used to construct commands for OpenBUGS from R enviroment
#glmmBUGS will be used to construct adjacency matrix from spatial data
#rgdal will be used to load spatial data
vietnam <- readOGR("data", "vn_utm") #Load vietnam map
vietnam <- vietnam[order(vietnam$VARNAME_2),] #Sort provinces by alphabe
vietnam$code <- seq_len(63) # Add numeric code (from 1 to 63 ) into the sorted provinces by alphabe
vietnam_nb <- poly2nb(vietnam, row.names = vietnam[["code"]]) #Calculate neighbors for each province
num <- sapply(vietnam_nb, length) #Calculate the number of surrounding provinces for each province
sumNumWeigh <- sum(num) # A sum of surrounding provinces
num <- paste(as.character(num), collapse = ",") #Transform a numeric vector to a character separated by comma
adj <- sapply(vietnam_nb, function(x) {
paste(c(as.character(x),""), collapse = ",")
})
last_index <- length(adj)
adj[last_index] <- gsub(",$","",adj[last_index])
#Writing to a text file
sink("adjacency.txt") #Open the file to write out
cat("list(num = c(",num,"),\nadj = c(\n", sep = "")
writeLines(adj, con = stdout())
cat("),\n")
cat("sumNumNeigh =",sumNumWeigh,")", sep = "")
sink()
#Write out a reference file of province name and code value
sorted_province <- with(vietnam@data, data.frame(code=code, province=VARNAME_2))
write.csv(sorted_province, file = "provinces.txt", row.names = F, quote = F)
# The adjecency should be like below
# list(num = c( 3,2,6,4,4,4,3,3,4,5,4,2,5,4,2,3,5,2,6,5,5,4,6,8,2,6,3,5,5,6,5,4,5,3,4,7,5,3,4,3,2,4,3,6,4,2,4,4,4,2,4,6,4,5,6,4,3,5,3,7,7,4,6 ),
# adj = c(
# 13,20,33,
# 11,19,
# 6,24,26,37,49,55,
# 14,37,55,60,
# 12,28,33,51,
# 3,24,26,31,
# 58,59,61,
# 21,45,48,
# 10,19,29,53,
# 9,16,19,36,53,
# 2,19,36,43,
# 5,33,
# 1,20,28,33,61,
# 4,22,37,60,
# 47,57,
# 10,17,36,
# 16,21,32,36,45,
# 35,52,
# 2,9,10,11,29,36,
# 1,13,39,58,61,
# 8,17,34,45,48,
# 14,38,60,63,
# 24,30,31,40,42,54,
# 3,6,23,30,31,44,55,62,
# 41,46,
# 3,6,27,31,49,54,
# 26,49,54,
# 5,13,33,51,61,
# 9,19,39,53,58,
# 23,24,42,44,52,56,
# 6,23,24,26,54,
# 17,36,43,45,
# 1,5,12,13,28,
# 21,47,48,
# 18,38,52,63,
# 10,11,16,17,19,32,43,
# 3,4,14,49,55,
# 22,35,63,
# 20,29,53,58,
# 23,42,54,
# 25,56,
# 23,30,40,56,
# 11,32,36,
# 24,30,52,60,62,63,
# 8,17,21,32,
# 25,50,
# 15,34,48,57,
# 8,21,34,47,
# 3,26,27,37,
# 46,57,
# 5,28,59,61,
# 18,30,35,44,56,63,
# 9,10,29,39,
# 23,26,27,31,40,
# 3,4,24,37,60,62,
# 30,41,42,52,
# 15,47,50,
# 7,20,29,39,61,
# 7,51,61,
# 4,14,22,44,55,62,63,
# 7,13,20,28,51,58,59,
# 24,44,55,60,
# 22,35,38,44,52,60
# ),
# sumNumNeigh = 268 )
#Code and province
# code,province
# 1,An Giang
# 2,Ba Ria - VTau|Ba Ria-Vung Tau
# 3,Bac Giang
# 4,Bac Kan|Bac Can
# 5,Bac Lieu
# 6,Bac Ninh
# 7,Ben Tre
# 8,Binh Dinh
# 9,Binh Duong
# 10,Binh Phuoc
# 11,Binh Thuan
# 12,Ca Mau
# 13,Can Tho
# 14,Cao Bang
# 15,Da Nang City|Da Nang
# 16,Dac Nong
# 17,Dak Lak|Dac Lac
# 18,Dien Bien
# 19,Dong Nai
# 20,Dong Thap
# 21,Gia Lai
# 22,Ha Giang
# 23,Ha Nam
# 24,Ha Noi City|Hanoi
# 25,Ha Tinh
# 26,Hai Duong
# 27,Hai Phong City|Haiphong
# 28,Hau Giang
# 29,Ho Chi Minh City|Ho Chi Minh
# 30,Hoa Binh
# 31,Hung Yen
# 32,Khanh Hoa
# 33,Kien Giang
# 34,Kon Tum
# 35,Lai Chau
# 36,Lam Dong
# 37,Lang Son
# 38,Lao Cai
# 39,Long An
# 40,Nam Dinh
# 41,Nghe An
# 42,Ninh Binh
# 43,Ninh Thuan
# 44,Phu Tho
# 45,Phu Yen
# 46,Quang Binh
# 47,Quang Nam
# 48,Quang Ngai
# 49,Quang Ninh
# 50,Quang Tri
# 51,Soc Trang
# 52,Son La
# 53,Tay Ninh
# 54,Thai Binh
# 55,Thai Nguyen
# 56,Thanh Hoa
# 57,Thua Thien - Hue
# 58,Tien Giang
# 59,Tra Vinh
# 60,Tuyen Quang
# 61,Vinh Long
# 62,Vinh Phuc
# 63,Yen Bai
以上是关于r 一个简单的R脚本,用于在Win / Open BUGS中创建在OpenBUGS中运行的邻接数据的主要内容,如果未能解决你的问题,请参考以下文章