in some circumstances, adding a country-sample-specific identifier to the psu and strata variables would be enough to maintain the svydesign
calculations. if you stacked the datasets from those four nations, would the sum of weights match their relative populations? otherwise you might want to postStratify
?
set.seed(1999)
library(survey)
x <- data.frame( psu_variable = sample( 1:3 , 200 , replace = TRUE ) , strata_variable = sample( 1:10 , 200 , replace = TRUE ) , weight_variable = sample( 1:2 , 200 , replace = TRUE ) , some_variable = sample( 1:50 , 200 , replace = TRUE ) )
y <- data.frame( psu_variable = sample( 1:3 , 200 , replace = TRUE ) , strata_variable = sample( 1:10 , 200 , replace = TRUE ) , weight_variable = sample( 1:2 , 200 , replace = TRUE ) , some_variable = sample( 1:50 , 200 , replace = TRUE ) )
# first country
# x
x[ , 'country_name' ] <- 'first country'
# second country
# y
y[ , 'country_name' ] <- 'second country'
# possible design for first country
x_des <- svydesign( ~ psu_variable , strata = ~ strata_variable , data = x , weights = ~ weight_variable , nest = TRUE )
# possible design for second country
y_des <- svydesign( ~ psu_variable , strata = ~ strata_variable , data = y , weights = ~ weight_variable , nest = TRUE )
# add a unique country identifier to psu and strata variables
x[ , 'new_psu_variable' ] <- paste( "first country" , x[ , 'psu_variable' ] )
y[ , 'new_psu_variable' ] <- paste( "second country" , y[ , 'psu_variable' ] )
x[ , 'new_strata_variable' ] <- paste( "first country" , x[ , 'strata_variable' ] )
y[ , 'new_strata_variable' ] <- paste( "second country" , y[ , 'strata_variable' ] )
# stack and create possible design
needed_variables <- c( 'new_psu_variable' , 'new_strata_variable' , 'weight_variable' , 'country_name' , 'some_variable' )
z <- rbind( x[ needed_variables ] , y[ needed_variables ] )
# possible stacked design
z_des <- svydesign( ~ new_psu_variable , strata = ~ new_strata_variable , data = z , weights = ~ weight_variable , nest = TRUE )
# these statistics definitely need to line up
svymean( ~ some_variable , x_des )
svymean( ~ some_variable , y_des )
svyby( ~ some_variable , ~ country_name , z_des , svymean )
# combined result assumes x and y had appropriate weights
svymean( ~ some_variable , z_des )
# do the weights match the country populations?
sum( x[ , 'weight_variable' ] )
sum( y[ , 'weight_variable' ] )
# otherwise, post-stratify the design so it matches the relative population sizes
pop.types <- data.frame( country_name = c( 'first country' , 'second country' ) , Freq = c( 250000 , 750000 ) )
z_des_p <- postStratify( z_des , ~ country_name , pop.types )
# within-country estimates do not change
svyby( ~ some_variable , ~ country_name , z_des , svymean )
svyby( ~ some_variable , ~ country_name , z_des_p , svymean )
# combined country estimates now reflect relative population sizes for the `z_des_p` survey design
svymean( ~ some_variable , z_des )
svymean( ~ some_variable , z_des_p )