packages required for data processing and analysis
library(fst)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(missForest)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(mice)
##
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
##
## filter
## The following objects are masked from 'package:base':
##
## cbind, rbind
Missing value Analysis
data <- read_fst("sampled_data.fst")
# Extract specific variables
sampled_data <- data %>% select(netuse, netusoft, netustm, ppltrst, nwspol)
# Calculate the number of missing values for each variable
missing_values <- sapply(sampled_data, function(x) sum(is.na(x)))
# Calculate the percentage of missing values for each variable
missing_percentage <- missing_values / nrow(sampled_data) * 100
# Print the percentage of missing values
print(missing_percentage)
## netuse netusoft netustm ppltrst nwspol
## 52.105 68.765 68.765 0.000 68.765
Filling in missing values
# Use missForest to impute missing values
imputed_data <- missForest(sampled_data)
# Get the imputed data
completed_data <- imputed_data$ximp
# View the imputed data
print(head(completed_data))
## netuse netusoft netustm ppltrst nwspol
## 1 0.00 3.643333 130.3073 1 311.0345
## 2 4.00 4.030000 221.1673 5 720.0510
## 3 6.00 4.000000 242.7500 7 1843.2709
## 4 47.64 5.000000 90.0000 7 60.0000
## 5 47.66 5.000000 60.0000 7 60.0000
## 6 1.00 3.480000 1150.5975 4 195.1134
Building a linear regression model
# Build a linear regression model, with netuse as the dependent variable and netusoft as the independent variable
linear_model1 <- lm(netuse ~ netusoft, data = completed_data)
# Output the model summary
summary(linear_model1)
##
## Call:
## lm(formula = netuse ~ netusoft, data = completed_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.17 -14.10 -10.07 29.39 80.83
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.0468 0.6498 12.38 <2e-16 ***
## netusoft 1.9968 0.1617 12.35 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 20.13 on 19998 degrees of freedom
## Multiple R-squared: 0.00757, Adjusted R-squared: 0.007521
## F-statistic: 152.5 on 1 and 19998 DF, p-value: < 2.2e-16
Analyze another linear regression model
# Output the model summary
summary(linear_model1)
##
## Call:
## lm(formula = netuse ~ netusoft, data = completed_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.17 -14.10 -10.07 29.39 80.83
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.0468 0.6498 12.38 <2e-16 ***
## netusoft 1.9968 0.1617 12.35 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 20.13 on 19998 degrees of freedom
## Multiple R-squared: 0.00757, Adjusted R-squared: 0.007521
## F-statistic: 152.5 on 1 and 19998 DF, p-value: < 2.2e-16
# Build a linear regression model, with netuse as the dependent variable and netustm as the independent variable
linear_model2 <- lm(netuse ~ netustm, data = completed_data)
Exploring Linear Regression Model 3
# Output the model summary
summary(linear_model2)
##
## Call:
## lm(formula = netuse ~ netustm, data = completed_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -18.132 -10.748 -6.650 1.643 46.309
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.116e+01 1.410e-01 79.12 <2e-16 ***
## netustm 5.042e-03 6.720e-05 75.03 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 17.85 on 19998 degrees of freedom
## Multiple R-squared: 0.2197, Adjusted R-squared: 0.2196
## F-statistic: 5630 on 1 and 19998 DF, p-value: < 2.2e-16
linear_model3 <- lm(ppltrst ~ nwspol * netuse, data = completed_data)
summary(linear_model3)
##
## Call:
## lm(formula = ppltrst ~ nwspol * netuse, data = completed_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -21.060 -2.265 0.249 1.658 93.929
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.690e+00 6.656e-02 70.459 < 2e-16 ***
## nwspol 2.302e-04 2.929e-05 7.859 4.08e-15 ***
## netuse 1.122e-02 2.211e-03 5.074 3.94e-07 ***
## nwspol:netuse 1.447e-05 1.326e-06 10.918 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.843 on 19996 degrees of freedom
## Multiple R-squared: 0.0223, Adjusted R-squared: 0.02216
## F-statistic: 152.1 on 3 and 19996 DF, p-value: < 2.2e-16