Calculating net assets with missing values: discrepancy in results

I’m working with a wealth component dataset that includes variables for housing, business, financial assets, loans, and non-housing loans. These variables have varying levels of randomly allocated missing values (NA).

My goal is to calculate net assets using the formula: housing + business + financial – loan – loan_non_housing

I’ve implemented the following method to calculate net assets. It handles missing values (NA) carefully. For assets and liabilities, it sums components while ignoring NAs. Then, returns NA if all components are NA (instead of 0). For net assets, the code accounts for all combinations of NA in assets and liabilities. It also gives accurate results even when only one value is available.

df$assets <- apply(df[, c("housing","business", "financial")], 1, function(x) {
  if (all(is.na(x))) { return(NA)} else {return(sum(x, na.rm = TRUE)) }})

df$liabilities <- apply(df[, c("loan","loan_non_housing")], 1, function(x) {
  if (all(is.na(x))) { return(NA)} else {return(sum(x, na.rm = TRUE)) }})

df$net_assets <- ifelse(is.na(df$assets) & is.na(df$liabilities), NA,
                        ifelse(is.na(df$assets), -df$liabilities,
                               ifelse(is.na(df$liabilities), df$assets,
                                      df$assets - df$liabilities)))

This approach yielded more accurate results compared to simpler calculation methods.

However, I’ve encountered an issue when calculating the average wealth by age groups and social classes:

fig_net_assets <- df %>%
  group_by(age_groups, social_class) %>%
  summarise(
    mean_net_assets = mean(net_assets, na.rm = TRUE),
    mean_net_assets_manual = mean(housing + business + financial - loan - loan_non_housing, na.rm = TRUE),
    mean_housing = mean(housing, na.rm = TRUE),
    mean_business = mean(business, na.rm = TRUE),
    mean_financial = mean(financial, na.rm = TRUE),
    mean_loan = mean(loan, na.rm = TRUE),
    mean_loan_non_housing = mean(loan_non_housing, na.rm = TRUE)
  )

The results from mean_net_assets = mean(net_assets, na.rm = TRUE) differ significantly from the manually calculated total of mean averages (mean_housing + mean_business + mean_financial - mean_loan - mean_loan_non_housing).

In my actual dataset, the mean_net_assets results are more intuitive. For example, for the working class aged 25-34 in Germany, I get an average wealth of around 50K euros when i calculate it with mean_net_assets. On the other hand, i get 200K euros when i calculate it with mean_net_assets_manual. The latter scenario is very much unlikely compared to other findings in the literature which makes me believe that the way i calculated mean_net_assets for missing values earlier is correct.

My question is: How can I calculate the average wealth for each dimension separately while ensuring that their sum (mean_housing + mean_business + mean_financia - mean_loan - mean_loan_non_housing) matches the overall average net assets calculated using mean(net_assets, na.rm = TRUE)? While keeping in mind that i have to keep the same operationalization for net_assets to handles all missing values correctly.

I’ve included a reproducible dataset and sample code below for reference:

Any insights or suggestions would be greatly appreciated!

Reproducible code:

library(dplyr)
library(tidyr)

set.seed(123)  # for reproducibility

# Create base dataset
n <- 10000  # number of observations

df <- data.frame(
  age_groups = sample(c("25-34", "35-44", "45-54", "55-64"), n, replace = TRUE, 
                      prob = c(0.3, 0.3, 0.2, 0.2)),
  social_class = sample(c("Self-employed", "Salariat", "Intermediate", "Working class"), n, replace = TRUE, 
                        prob = c(0.1, 0.3, 0.3, 0.3))
)

# Function to generate wealth based on age and social class
generate_wealth <- function(age_group, social_class, base_mean, base_sd) {
  age_factor <- case_when(
    age_group == "25-34" ~ 0.5,
    age_group == "35-44" ~ 0.8,
    age_group == "45-54" ~ 1.2,
    age_group == "55-64" ~ 1.5
  )
  
  class_factor <- case_when(
    social_class == "Working class" ~ 0.6,
    social_class == "Intermediate" ~ 1.0,
    social_class == "Salariat" ~ 1.4,
    social_class == "Self-employed" ~ 1.2
  )
  
  wealth <- rnorm(1, mean = base_mean * age_factor * class_factor, sd = base_sd)
  return(max(wealth, 0))  # Ensure non-negative values
}

# Generate wealth variables
df <- df %>%
  rowwise() %>%
  mutate(
    housing = generate_wealth(age_groups, social_class, 200000, 50000),
    business = generate_wealth(age_groups, social_class, 50000, 20000),
    financial = generate_wealth(age_groups, social_class, 100000, 30000),
    loan = generate_wealth(age_groups, social_class, 20000, 10000),
    loan_non_housing = generate_wealth(age_groups, social_class, 150000, 50000)
  ) %>%
  ungroup()

# Function to introduce missing values
introduce_missing <- function(x, rate) {
  n <- length(x)
  missing_indices <- sample(1:n, size = round(n * rate), replace = FALSE)
  x[missing_indices] <- NA
  return(x)
}

# Introduce missing values
df <- df %>%
  mutate(
    housing = introduce_missing(housing, 0.20),
    business = introduce_missing(business, 0.1),
    financial = introduce_missing(financial, 0.25),
    loan = introduce_missing(loan, 0.20),
    loan_non_housing = introduce_missing(loan_non_housing, 0.15)
  )


# Display missing value percentages
missing_percentages <- df %>%
  summarise(across(everything(), ~mean(is.na(.)) * 100)) %>%
  pivot_longer(everything(), names_to = "Variable", values_to = "Missing_Percentage")


# Calculate, assets, liabilities and net assets

df$assets <- apply(df[, c("housing","business", "financial")], 1, function(x) {
  if (all(is.na(x))) { return(NA)} else {return(sum(x, na.rm = TRUE)) }})


df$liabilities <- apply(df[, c("loan","loan_non_housing")], 1, function(x) {
  if (all(is.na(x))) { return(NA)} else {return(sum(x, na.rm = TRUE)) }})

df$net_assets <- ifelse(is.na(df$assets) & is.na(df$liabilities), NA,
                        ifelse(is.na(df$assets), -df$liabilities,
                               ifelse(is.na(df$liabilities), df$assets,
                                      df$assets - df$liabilities)))



# Create the mean by social class and age groups
fig_net_assets <- df %>%
  group_by(age_groups, social_class) %>%
  summarise(
    mean_net_assets = mean(net_assets, na.rm = TRUE), # this line of code has to be compared with the line below it
    mean_net_assets_manual = mean(housing + business + financial - loan - loan_non_housing, na.rm = TRUE), # this line of code has to be compared with the line above it
    mean_housing = mean(housing, na.rm = TRUE),
    mean_business = mean(business, na.rm = TRUE),
    mean_financial = mean(financial, na.rm = TRUE),
    mean_loan = mean(loan, na.rm = TRUE),
    mean_loan_non_housing = mean(loan_non_housing, na.rm = TRUE)
  )

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật