new_data <- data.frame(Corruption_index = Country_test[1,2],
                       GDP_per_capita = Country_test[1,3])

prediction_interval <- predict(model,
                               newdata = new_data,
                               interval = "prediction",
                               level = 0.9)
print(prediction_interval)
##        fit     lwr     upr
## 1 5.464745 4.61857 6.31092
Lower <- prediction_interval[1,2]
Upper <- prediction_interval[1,3]
Fit <- prediction_interval[1,1]
Real <- Country_test[1,4]
summary_table <- cbind(Lower, Fit, Upper,Real)
colnames(summary_table) <- c('Lower', 'Fit', 'Upper','Real')
rownames(summary_table) <- c('Chosen country')
kable(summary_table, caption="Confidence Interval of Predicted Nation",
      digits = c(4,4,4,4), align = 'cccc')
Confidence Interval of Predicted Nation
Lower Fit Upper Real
Chosen country 4.6186 5.4647 6.3109 5.2

If we take the estimate and take that and times the corresponding number of the variable, we will have the happiness index that is equivalent to the happiness index in the dataset.

Confidence Interval of Predicted Nation
Lower Fit Upper Real Manual fit
Chosen country 4.6186 5.4647 6.3109 5.2 5.4647

The manual result is exactly the same the resulted taken from the predict function of R.

#Confidence interval of the sample mean = 95% through the t.test

# Create two example data frames
lower_1 <- t.test(GDP_no_outliers$GDP_growth_rate)$conf.int[1]
upper_1 <- t.test(GDP_no_outliers$GDP_growth_rate)$conf.int[2]
lower_2 <- t.test(GDP_no_outliers$GDP_per_capita)$conf.int[1]
upper_2 <- t.test(GDP_no_outliers$GDP_per_capita)$conf.int[2]
lower_3 <- t.test(GDP_no_outliers$Inflation_rate)$conf.int[1]
upper_3 <- t.test(GDP_no_outliers$Inflation_rate)$conf.int[2]

fit_1 <- t.test(GDP_no_outliers$GDP_growth_rate)$estimate
fit_2 <- t.test(GDP_no_outliers$GDP_per_capita)$estimate
fit_3 <- t.test(GDP_no_outliers$Inflation_rate)$estimate

A <- c(lower_1,fit_1,upper_1)
B <-  c(lower_2,fit_2,upper_2)
C <-  c(lower_3,fit_3,upper_3)

table_1 <- rbind(A,B,C)

rownames(table_1) <- c('GDP growth rate','GDP per capita','Inflation rate')
colnames(table_1) <- c('Lower','Sample','Upper')
kable(table_1, caption="Confidence Interval of sample mean with 95% confidence",
      digits = c(4,4,4), align = 'ccc')
Confidence Interval of sample mean with 95% confidence
Lower Sample Upper
GDP growth rate 3.0759 4.1286 5.1813
GDP per capita 10.2692 15.7771 21.2851
Inflation rate 7.3731 8.7886 10.2040
#Draw table
table_2 <- rbind(confidence_interval_1, confidence_interval_2, confidence_interval_3)
rownames(table_2) <- c('GDP growth rate','GDP per capita','Inflation rate')
colnames(table_2) <- c('Lower','Upper')

kable(table_1, caption="Confidence Interval of sample mean with 95% confidence",
      digits = c(4,4,4), align = 'ccc')
Confidence Interval of sample mean with 95% confidence
Lower Sample Upper
GDP growth rate 3.0759 4.1286 5.1813
GDP per capita 10.2692 15.7771 21.2851
Inflation rate 7.3731 8.7886 10.2040
kable(table_2, caption="Confidence Interval of sample mean calculated manually",
      digits = c(4,4,4), align = 'ccc')
Confidence Interval of sample mean calculated manually
Lower Upper
GDP growth rate 3.2792 4.9780
GDP per capita 11.3328 20.2215
Inflation rate 7.6465 9.9307

After all of those calculations, we can see that the sample mean and confidence interval from t.test() is quite equivalent tothe manual calculations we have been making, but the latter is much more precise than t.test().