Skip to content

Instantly share code, notes, and snippets.

@djhurio
Last active November 1, 2016 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djhurio/071f89d3961a90a9401f1333db303cac to your computer and use it in GitHub Desktop.
Save djhurio/071f89d3961a90a9401f1333db303cac to your computer and use it in GitHub Desktop.
Mate5054 - Apsekojumu statistika (2KP). Trešais praktiskais darbs
### Apsekojumu statistika
### Praktiskie darbi 3
# Bibliotekas ####
# Bibliotēku instalācija, ja nepieciešams
# install.packages(c("Rcpp", "BalacedSampling", "sampling"))
require(data.table)
require(sampling)
require(BalancedSampling)
# Nepieciešamās direktorijas
dir.data <- "~/ApsStat/Data"
dir.prd <- "~/ApsStat/PrDarbi3"
# Izveido direktorijas
system(paste("mkdir -p", dir.data))
system(paste("mkdir -p", dir.prd))
# Datu fails
file.data <- file.path("http://home.lu.lv/~pm90015/work/LU",
"ApsekojumuStatistika/Data/Population.Rdata")
# Dati ####
setwd(dir.data)
# Nolādē datu failu, ja nepieciešams
# download.file(file.data, "Population.Rdata")
load("Population.Rdata")
pop
pop <- pop[sample(.N, round(.N * .1))]
pop
pop[, .N, keyby = J100]
# Pārkodējam J100 vērtības
pop[is.na(J100) | J100 == 9, J100 := 2]
pop[, .N, keyby = J100]
# SRS ####
n <- 2000
# Izlasē iekļaušanas varbūtības
pop[, pik := n / .N]
pop[, sum(pik)]
# SRS ####
pop[, s1 := srswor(n, .N)]
pop[, sum(s1)]
# Īstās populācijas vērtības
Y <- pop[, list(Y = .N), keyby = eka]
Y
# HT novērtējums
Y_SRS <- pop[, list(Y_SRS = sum(s1 / pik)), keyby = eka]
Y_SRS
test1 <- merge(Y, Y_SRS)[, diff := Y_SRS - Y]
test1[, rel_diff := diff / Y]
test1
# Balansētā izlase pēc eka ####
# Fiktīvie mainīgie katrai eka kategorijai
pop[, eka1 := as.integer(eka == 1L)]
pop[, eka2 := as.integer(eka == 2L)]
pop[, eka3 := as.integer(eka == 3L)]
# sampling::samplecube
# Atgriež vektoru ar garumu N un vērtībām 0 vai 1
pop[, s2 := samplecube(X = cbind(eka1, eka2, eka3), pik = pik)]
pop[, sum(s2)]
# BalancedSampling::cube
# Atgriež vektoru ar garumu n, kas satur izlasē iekļauto elementu numurus
# Kā pirmo kolonu Xbal vajag iekļaut varbūtību vektoru,
# lai nodrošinātu fiksētu izlases apjomu
pop[, cube(prob = pik, Xbal = cbind(pik, eka1, eka2, eka3))]
# Aprēķinam izlases indikatoru
pop[, s3 := as.integer(.I %in% cube(prob = pik,
Xbal = cbind(pik, eka1, eka2, eka3)))]
pop[, sum(s3)]
# HT novērtējums
Y_Bal2 <- pop[, list(Y_Bal2 = sum(s2 / pik)), keyby = eka]
Y_Bal2
Y_Bal3 <- pop[, list(Y_Bal3 = sum(s3 / pik)), keyby = eka]
Y_Bal3
test2 <- merge(Y, Y_Bal2)[, diff := Y_Bal2 - Y]
test2[, diff_rel := diff / Y]
test3 <- merge(Y, Y_Bal3)[, diff := Y_Bal3 - Y]
test3[, diff_rel := diff / Y]
test1
test2
test3
# Literatūras saraksts ####
citation()
citation("sampling")
citation("BalancedSampling")
citation("data.table")
RStudio.Version()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment