single_lm2: Linear Regression Analysis for Specified Location

View source: R/hello.R

single_lm2R Documentation

Linear Regression Analysis for Specified Location

Description

This function runs a linear regression model for a specified location within a dataset.

Usage

single_lm2(
  data,
  formula,
  location_var,
  response_distribution = "normal",
  family = NULL,
  location_index
)

Arguments

data

The dataset to be analyzed.

formula

The formula for the regression model.

location_var

The variable indicating different locations (e.g., 'REGCODE').

response_distribution

The distribution of the response variable ("normal" for normal distribution, "other" for other distributions).

family

The family to be used for GLM if response_distribution is "other" (e.g., 'binomial' for logistic regression).

location_index

The specific location index or number for which the model should be run.

Value

A dataframe containing the results for the specified location.

Examples

set.seed(123)
if (requireNamespace("dplyr", quietly = TRUE)) {
  library(dplyr)
# Create dummy data
dummy_data <- data.frame(
  years_education = rnorm(100, 12, 3),    # Represents years of education
  gender_female = rbinom(100, 1, 0.5),    # 1 = Female, 0 = Male
  household_wealth = sample(1:5, 100, replace = TRUE),  # Wealth index from 1 to 5
  district_code = sample(1:10, 100, replace = TRUE)     # Represents district codes
) %>% arrange(district_code)

# Define a simple regression formula
formula <- years_education ~ gender_female + household_wealth + household_wealth:gender_female

# Run the regression for a specific location (e.g., district 1)
result_single_lm <- single_lm2(dummy_data, formula, "district_code",
 response_distribution = "normal", location_index = 1)

# View the result
print(result_single_lm)
}

DHSr documentation built on April 4, 2025, 12:18 a.m.