में जटिल सर्वेक्षण डिज़ाइन का उपयोग करके प्रश्नों की गति में सुधार करें मेरे पास एक बड़ा डेटा सेट (20 मिलियन से अधिक जुनून) है जिसका मैं survey
पैकेज के साथ विश्लेषण करता हूं और यह मुझे सरल प्रश्नों को चलाने के लिए उम्र ले रहा है। मैंने अपने कोड को तेज़ करने का एक तरीका खोजने का प्रयास किया है, लेकिन मैं यह जानना चाहता हूं कि इसे और अधिक कुशल बनाने के बेहतर तरीके हैं या नहीं। foreach
dopar
7 कोर आर
- सरल कमांड
svyby
/svytotal
- समानांतर कंप्यूटिंग:
मेरी बेंचमार्क में, मैं का उपयोग कर तीन आदेशों की गति
svyby
/svytotal
तुलना 2
स्पोइलर: विकल्प 3 पहले विकल्प के रूप में तेज़ी से दोगुना से अधिक है लेकिन यह बड़े डेटा सेट के लिए उपयुक्त नहीं है क्योंकि यह समांतर कंप्यूटिंग पर निर्भर करता है, जो बड़े डेटा सेट से निपटने के दौरान त्वरित रूप से स्मृति सीमाओं को दबाता है। मुझे 16 जीबी रैम के बावजूद भी इस समस्या का सामना करना पड़ता है। कुछ solutions to this memory limitation हैं, लेकिन उनमें से कोई सर्वेक्षण डिज़ाइन ऑब्जेक्ट्स पर लागू नहीं है।
मेमोरी सीमाओं के कारण इसे तेज बनाने और क्रैश न करने के बारे में कोई विचार?
एक प्रतिलिपि प्रस्तुत करने योग्य उदाहरण के साथ मेरे कोड:
# Load Packages
library(survey)
library(data.table)
library(compiler)
library(foreach)
library(doParallel)
options(digits=3)
# Load Data
data(api)
# Convert data to data.table format (mostly to increase speed of the process)
apiclus1 <- as.data.table(apiclus1)
# Multiplicate data observations by 1000
apiclus1 <- apiclus1[rep(seq_len(nrow(apiclus1)), 1000), ]
# create a count variable
apiclus1[, Vcount := 1]
# create survey design
dclus1 <- svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
1) सरल कोड
t1 <- Sys.time()
table1 <- svyby(~Vcount,
~stype+dnum+cname,
design = dclus1,
svytotal)
T1 <- Sys.time() - t1
2) 7 कोर
# in this option, I create a list with different subsets of the survey design
# that will be passed to different CPU cores to work at the same time
subdesign <- function(i){ subset(dclus1, dnum==i)}
groups <- unique(apiclus1$dnum)
list_subsets <- lapply(groups[], subdesign) # apply function and get all subsets in a list
i <- NULL
# Start Parallel
registerDoParallel(cores=7)
t2 <- Sys.time()
table2 <- foreach (i = list_subsets, .combine= rbind, .packages="survey") %dopar% {
options(survey.lonely.psu = "remove")
svyby(~Vcount,
~stype+dnum+cname,
design = i,
svytotal)}
T2 <- Sys.time() - t2
3 का उपयोग कर foreach dopar साथ समानांतर कंप्यूटिंग। विकल्प 2
का एक संकलित संस्करण# make a function of the previous query
query2 <- function (list_subsets) { foreach (i = list_subsets, .combine= rbind, .packages="survey") %dopar% {
svyby(~Vcount,
~stype+dnum+cname,
design = i,
svytotal)}}
# Compile the function to increase speed
query3 <- cmpfun(query2)
t3 <- Sys.time()
table3 <- query3 (list_subsets)
T3 <- Sys.time() - t3
परिणाम इतनी अच्छी तरह से बाहर इस सवाल बिछाने के लिए
>T1: 1.9 secs
>T2: 1.13 secs
>T3 0.58 secs
barplot(c(T1, T2, T3),
names.arg = c("1) simple table", "2) parallel", "3) compiled parallel"),
ylab="Seconds")
समानांतर प्रसंस्करण के लिए प्रतिलिपि बनाये बिना डेटा को सब्सक्राइब करने के विकल्प के लिए पैकेज 'रेफरी' से 'refdata' देखें। –
मैंने refdata @ A.Webb को आजमाया है लेकिन यह काम नहीं किया है। कोड धीमा हो गया और यह अभी भी स्मृति सीमा को मार रहा है। मैं कुछ गलत कर रहा हूं 'समूह <- अद्वितीय (apiclus1 $ dnum) उपडिज़ाइन <- फ़ंक्शन (i) {refdata (सबसेट (dclus1, dnum == i))} list_subsets <- लापता (समूह [], subdesign) i <- NULL तालिका 3 <- foreach (i = 1: लंबाई (समूह), .combine = rbind, .packages = c ("सर्वेक्षण", "ref"))% dopar% { विकल्प (सर्वेक्षण .lonely.psu = "हटाने") svyby (~ Vcount, ~ stype + dnum + CNAME, डिजाइन = derefdata (list_subsets [[मैं]]), svytotal)} ' –
@RafaelPereira का उपयोग' MonetDB.R' और 'सर्वेक्षण' एक साथ। उदाहरण के लिए, https://github.com/ajdamico/asdfree/search?utf8=%E2%9C%93&q=MonetDB.R –