FIrstly we load all the necessary libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(dplyr)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine

LOading the DAta

library(readr)
 dive <- read_csv("dive.csv")
## Rows: 1904 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Country_Name, Country_Code, Clean_Fuel, Rural, Total, Urban, Energy...
## dbl (5): Time, Renewable_output, Renewable_share, Renewable_consumption, Tot...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
 head(dive)
## # A tibble: 6 × 14
##   Country_Name Country_Code  Time Clean_Fuel Rural  Total Urban Energy_intensity
##   <chr>        <chr>        <dbl> <chr>      <chr>  <chr> <chr> <chr>           
## 1 Afghanistan  AFG           2007 15.99      18.91… 33.7… 81.8… 1.460937575     
## 2 Afghanistan  AFG           2008 17.44      32.5   42.4  89.9  2.147589912     
## 3 Afghanistan  AFG           2009 18.84      31.84… 44.8… 85.3… 2.617060398     
## 4 Afghanistan  AFG           2010 20.68      32.4   42.7  82.8  2.939936558     
## 5 Afghanistan  AFG           2011 22.33      33.38… 43.2… 86.5… 3.755923312     
## 6 Afghanistan  AFG           2012 24.08      63.8   69.1  95    2.984757176     
## # ℹ 6 more variables: Renewable_output <dbl>, Renewable_share <dbl>,
## #   Renewable_consumption <dbl>, Renewable_total_share <chr>,
## #   Total_electricity_output <chr>, Total_consumption <dbl>

Making a set of the combination of variables

SET 1 : Renewables

# Create a subset of columns related to review ratings
view_data <- dive[, c("Clean_Fuel", "Renewable_output", "Renewable_share", "Renewable_consumption", "Renewable_total_share")]

# Convert non-numeric columns to numeric
view_data$Clean_Fuel <- as.numeric(view_data$Clean_Fuel)
## Warning: NAs introduced by coercion
view_data$Renewable_output <- as.numeric(view_data$Renewable_output)
view_data$Renewable_share <- as.numeric(view_data$Renewable_share)
view_data$Renewable_consumption <- as.numeric(view_data$Renewable_consumption)
view_data$Renewable_total_share <- as.numeric(view_data$Renewable_total_share)
## Warning: NAs introduced by coercion
view_data$Average_Renewable <- rowMeans(view_data[, c("Renewable_output", "Renewable_share", "Renewable_consumption", "Renewable_total_share")])

response_variable <- view_data$Average_Renewable

explainatory_variables <- view_data[, c("Renewable_output", "Renewable_share", "Renewable_consumption", "Renewable_total_share")]


head(view_data)
## # A tibble: 6 × 6
##   Clean_Fuel Renewable_output Renewable_share Renewable_consumption
##        <dbl>            <dbl>           <dbl>                 <dbl>
## 1       16.0              684            72                  15270.
## 2       17.4              541            68.7                15313.
## 3       18.8              775            87.2                16854.
## 4       20.7              767            86.0                19579.
## 5       22.3              829            82.5                20435.
## 6       24.1              878            85.9                22561.
## # ℹ 2 more variables: Renewable_total_share <dbl>, Average_Renewable <dbl>
head(response_variable)
## [1] 4015.091 3985.962 4433.545 5111.632 5339.545 5884.811
explan_variables <- view_data[, c("Renewable_output", "Renewable_share", "Renewable_consumption", "Renewable_total_share")]

# Now you can use head() on explanatory_variables
head(explan_variables)
## # A tibble: 6 × 4
##   Renewable_output Renewable_share Renewable_consumption Renewable_total_share
##              <dbl>           <dbl>                 <dbl>                 <dbl>
## 1              684            72                  15270.                  33.9
## 2              541            68.7                15313.                  21.3
## 3              775            87.2                16854.                  17.8
## 4              767            86.0                19579.                  14.8
## 5              829            82.5                20435.                  11.5
## 6              878            85.9                22561.                  14.0
# Hypothesis: Considering reviews of Energy, more Energytic tends to be more appreciated and deserve more number reviews. Let's find out

# Scatterplot for Average Renewable consumption vs. Renewable share
plot(explan_variables$Renewable_consumption, response_variable, main = "Renewable consumption",
     xlab = "Renewable Consumption", ylab = "Average Renewable Consumption")

# Add a regression line
abline(lm(response_variable ~ explan_variables$Renewable_consumption), col = "red")

cat("Based on the scatterplot, there appears to be a positive relationship  rating tends to increase as well.")
## Based on the scatterplot, there appears to be a positive relationship  rating tends to increase as well.
## Based on the scatterplot, there appears to be a positive relationship between Energy and average rating. As Energy rating increases, average rating tends to increase as well.

cat("The red regression line suggests a strong positive linear trend.")
## The red regression line suggests a strong positive linear trend.
## The blue regression line suggests a strong positive linear trend.
# Create boxplots for review variables

boxplot(view_data[, c("Renewable_output", "Renewable_share", "Renewable_consumption", "Renewable_total_share")], 
        main = "Boxplots of Variables",
        ylab = "Energy rating",
        col = c("lightblue", "lightgreen", "lightpink", "lightyellow"),
        border = c("red", "blue", "green", "yellow"),
        names = c("Renewable_output", 'Renewable_share', 'Renewable_consumption', 'Renewable_total_share'))

cat("Box plot of Review variables showcases a lot of outliers for the review scores")
## Box plot of Review variables showcases a lot of outliers for the review scores
## Box plot of Review variables showcases a lot of outliers for the review scores
correlation_matrix <- cor(view_data, use = "complete.obs")

heatmap(correlation_matrix, 
        col = colorRampPalette(c("brown", "yellow", "green"))(60),
        main = "Correlation Heatmap",
        xlab = "Variables",
        ylab = "Variables")

library(boot)

boot_ci <- function (v, func = median, conf = 0.95, n_iter = 1000) {
  # Define the bootstrapped function
  boot_func <- function(x, i) {
    # Remove missing values (NA) within each bootstrap sample
    x_sample <- x[i]
    x_sample <- x_sample[!is.na(x_sample)]
    func(x_sample)
  }
  
  # Remove missing values (NA) from the original data
  v <- v[!is.na(v)]
  
  # Perform the bootstrap resampling
  b <- boot(data = v, statistic = boot_func, R = n_iter)
  
  # Compute the confidence intervals
  boot.ci(b, conf = conf, type = "perc")
}

# Usage
boot_ci(view_data$Average_Renewable, mean, 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = b, conf = conf, type = "perc")
## 
## Intervals : 
## Level     Percentile     
## 95%   ( 76917, 104626 )  
## Calculations and Intervals on Original Scale

Set 2: Energy characteristic

# Create a subset of columns related to review ratings
revi_data <- dive[, c("Rural", "Urban", "Total", "Energy_intensity")]

# Convert non-numeric columns to numeric
revi_data$Rural <- as.numeric(revi_data$Rural)
## Warning: NAs introduced by coercion
revi_data$Urban <- as.numeric(revi_data$Urban)
## Warning: NAs introduced by coercion
revi_data$Total <- as.numeric(revi_data$Total)
## Warning: NAs introduced by coercion
revi_data$Energy_intensity <- as.numeric(revi_data$Energy_intensity)
## Warning: NAs introduced by coercion
# Calculate the average renewable rating
revi_data$Energy <- rowMeans(revi_data[, c("Rural", "Urban", "Total", "Energy_intensity")])

# explanatory variable
resp_variable <- revi_data$Average_Renew
## Warning: Unknown or uninitialised column: `Average_Renew`.
revi_data$Average_Renew <- rowMeans(revi_data)

resp_variable <- revi_data$Average_Renew

# (individual ratings)
explanat_variables <- revi_data[, c("Rural", "Urban", "Total", "Energy_intensity")]


head(revi_data)
## # A tibble: 6 × 6
##   Rural Urban Total Energy_intensity Energy Average_Renew
##   <dbl> <dbl> <dbl>            <dbl>  <dbl>         <dbl>
## 1  18.9  81.8  33.7             1.46   34.0          34.0
## 2  32.5  89.9  42.4             2.15   41.7          41.7
## 3  31.8  85.4  44.9             2.62   41.2          41.2
## 4  32.4  82.8  42.7             2.94   40.2          40.2
## 5  33.4  86.6  43.2             3.76   41.7          41.7
## 6  63.8  95    69.1             2.98   57.7          57.7
head(resp_variable)
## [1] 33.98152 41.73690 41.16790 40.20998 41.73146 57.72119
head(explanat_variables)
## # A tibble: 6 × 4
##   Rural Urban Total Energy_intensity
##   <dbl> <dbl> <dbl>            <dbl>
## 1  18.9  81.8  33.7             1.46
## 2  32.5  89.9  42.4             2.15
## 3  31.8  85.4  44.9             2.62
## 4  32.4  82.8  42.7             2.94
## 5  33.4  86.6  43.2             3.76
## 6  63.8  95    69.1             2.98
# Hypothesis: Considering reviews of Energy, more Energytic tends to be more appreciated and deserve more number reviews. Let's find out
# Scatterplot for Average Renewable consumption vs. Renewable share 
plot(explanat_variables$Total, resp_variable, main="Energy usage", 
     xlab="Energy Characteristics", ylab="Average Renewable consumptio")
abline(lm(resp_variable ~ explanat_variables$Total), col="magenta")

The magenta regression line suggests a strong positive linear trend.

# Creating a boxplots for variables
boxplot(revi_data[, c("Rural", "Urban", "Total", "Energy_intensity")], 
        main = "Boxplots of Review Variables",
        ylab = "Consumption of energy",
        col = c("lightblue", "lightgreen", "lightpink", "lightyellow"),
        border = c("blue", "green", "red", "yellow"),
        names = c("Rural", "Urban", "Total", "Energy_intensity"))

corre_matrix <- cor(revi_data, use = "complete.obs")

heatmap(corre_matrix, 
        col = colorRampPalette(c("green", "blue", "red"))(50),
        main = "Correlation Matrix",
        xlab = "Variables",
        ylab = "Variables")

library(boot)
boot_ci <- function (v, func = median, conf = 0.95, n_iter = 1000) {
  # Define the bootstrapped function
  boot_func <- function(x, i) {
    # Remove missing values (NA) within each bootstrap sample
    x_sample <- x[i]
    x_sample <- x_sample[!is.na(x_sample)]
    func(x_sample)
  }
  
  # Remove missing values (NA) from the original data
  v <- v[!is.na(v)]
  
  # Perform the bootstrap resampling
  b <- boot(data = v, statistic = boot_func, R = n_iter)
  
  # Compute the confidence intervals
  boot.ci(b, conf = conf, type = "perc")
}

# Usage
boot_ci(revi_data$Average_Renew, mean, 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = b, conf = conf, type = "perc")
## 
## Intervals : 
## Level     Percentile     
## 95%   (61.23, 63.13 )  
## Calculations and Intervals on Original Scale
third variable explanation
rev_data <- dive[, c("Total_consumption", "Renewable_output", "Renewable_share", "Renewable_consumption", "Total_electricity_output")]

# Convert non-numeric columns to numeric
rev_data$Total_consumption <- as.numeric(rev_data$Total_consumption)
rev_data$Renewable_output <- as.numeric(rev_data$Renewable_output)
rev_data$Renewable_share <- as.numeric(rev_data$Renewable_share)
rev_data$Renewable_consumption <- as.numeric(rev_data$Renewable_consumption)
rev_data$Total_electricity_output <- as.numeric(rev_data$Total_electricity_output)
## Warning: NAs introduced by coercion
# Calculate the average renewable rating
rev_data$Average_output <- rowMeans(rev_data[, c(
  "Total_consumption", "Renewable_output", "Renewable_share", "Renewable_consumption", "Total_electricity_output")])

res_variable <- rev_data$Average_output
explain_variables <- rev_data[, c("Total_consumption", "Renewable_output", "Renewable_share", "Renewable_consumption", "Total_electricity_output")]


head(rev_data)
## # A tibble: 6 × 6
##   Total_consumption Renewable_output Renewable_share Renewable_consumption
##               <dbl>            <dbl>           <dbl>                 <dbl>
## 1            45096.              684            72                  15270.
## 2            71744.              541            68.7                15313.
## 3            94613.              775            87.2                16854.
## 4           131934.              767            86.0                19579.
## 5           177965.              829            82.5                20435.
## 6           161457.              878            85.9                22561.
## # ℹ 2 more variables: Total_electricity_output <dbl>, Average_output <dbl>
head(res_variable)
## [1] 12414.40 17690.92 22643.64 30651.47 40063.36 37200.89
head(explain_variables)
## # A tibble: 6 × 5
##   Total_consumption Renewable_output Renewable_share Renewable_consumption
##               <dbl>            <dbl>           <dbl>                 <dbl>
## 1            45096.              684            72                  15270.
## 2            71744.              541            68.7                15313.
## 3            94613.              775            87.2                16854.
## 4           131934.              767            86.0                19579.
## 5           177965.              829            82.5                20435.
## 6           161457.              878            85.9                22561.
## # ℹ 1 more variable: Total_electricity_output <dbl>
# Hypothesis: Let's find out

plot(explain_variables$Renewable_consumption, res_variable, main="Energy improvement", 
     xlab="Renewable_consumption", ylab="Total_consumption")
abline(lm(res_variable ~ explain_variables$Total_consumption), col="brown") 

cor_matrix <- cor(rev_data, use = "complete.obs")

heatmap(cor_matrix, 
        col = colorRampPalette(c("magenta", "green", "red"))(70),
        main = "Correlation Matrix Heatmap",
        xlab = "Variables",
        ylab = "Variables")

boxplot(rev_data[, c("Total_consumption", "Renewable_output", "Renewable_share", "Renewable_consumption", "Total_electricity_output")], 
        main = "Boxplots of Review Variables",
        ylab = "",
        col = c("lightblue", "lightgreen", "lightpink", "lightyellow","magenta"),
        border = c("blue", "green", "red", "yellow", "violet"),
        names = c("Total_consumption", "Renewable_output", "Renewable_share", "Renewable_consumption", "Total_electricity_output"))

library(boot)
boot_ci <- function (v, func = median, conf = 0.95, n_iter = 1000) {
  # Define the bootstrapped function
  boot_func <- function(x, i) {
    # Remove missing values (NA) within each bootstrap sample
    x_sample <- x[i]
    x_sample <- x_sample[!is.na(x_sample)]
    func(x_sample)
  }
  
  # Remove missing values (NA) from the original data
  v <- v[!is.na(v)]
  
  # Perform the bootstrap resampling
  b <- boot(data = v, statistic = boot_func, R = n_iter)
  
  # Compute the confidence intervals
  boot.ci(b, conf = conf, type = "perc")
}

# Usage
boot_ci(rev_data$Average_output, mean, 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = b, conf = conf, type = "perc")
## 
## Intervals : 
## Level     Percentile     
## 95%   (394900, 567430 )  
## Calculations and Intervals on Original Scale

Analysis: it shows that assumptions made by the bootstapping process came out to be true