R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# Load libraries
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
# Load Excel file data
data <- read_excel("ROBBY NASHARY - food_consumption.xlsx", sheet = "data")
# Create the utility function
utility_function <- function(dairy, misc) {
  return(sqrt(dairy * misc))
}
# Apply the utility function to the data
data$utility <- mapply(utility_function, data$w_dairy, data$w_misc)
# Display first few rows of the dataframe with utility column
head(data)
## # A tibble: 6 × 14
##   w_dairy w_proteins w_fruitveg w_flours w_misc p_dairy p_proteins p_fruitveg
##     <dbl>      <dbl>      <dbl>    <dbl>  <dbl>   <dbl>      <dbl>      <dbl>
## 1   0.184      0.378      0.193   0.159  0.0862   0.243       1.49      0.430
## 2   0.263      0.221      0.179   0.162  0.175    0.560       1.32      0.676
## 3   0.113      0.399      0.241   0.0342 0.213    0.268       1.15      0.411
## 4   0.150      0.353      0.215   0.160  0.121    0.334       1.53      0.610
## 5   0.163      0.241      0.277   0.152  0.167    0.408       1.57      0.362
## 6   0.109      0.664      0.108   0.0406 0.0787   0.549       3.78      0.497
## # ℹ 6 more variables: p_flours <dbl>, p_misc <dbl>, expfd <dbl>,
## #   n_adults <dbl>, n_kids <dbl>, utility <dbl>
# Load necessary libraries
library(readxl)
library(ggplot2)
# Load Excel file
data <- read_excel("ROBBY_NASHARY_food_consumption_with_utility.xlsx")
# Create scatter plot
ggplot(data, aes(x = w_dairy, y = w_misc, color = utility)) +
  geom_point() +
  labs(title = "Scatter Plot of Dairy vs Misc with Utility Value",
       x = "Dairy",
       y = "Misc",
       color = "Utility") +
  theme_minimal()

# Load necessary libraries
library(readxl)
library(ggplot2)
# Load grid data for plotting
grid_data <- read_excel("ROBBY_NASHARY_utility_grid_data.xlsx")
# Load the data with utility column
data <- read_excel("ROBBY_NASHARY_food_consumption_with_utility.xlsx")
# Calculate average prices
avg_price_dairy <- mean(data$p_dairy, na.rm = TRUE)
avg_price_misc <- mean(data$p_misc, na.rm = TRUE)

# Define a sample total expenditure value
total_expenditure <- mean(data$expfd, na.rm = TRUE)
# Create the budget constraint line
budget_constraint <- function(dairy) {
  return((total_expenditure - avg_price_dairy * dairy) / avg_price_misc)
}
# Create the indifference curves plot
ggplot(grid_data, aes(x = dairy, y = misc, color = utility)) +
  geom_point(alpha = 0.5) +
  stat_contour(aes(z = utility), bins = 10, color = "blue") +
  geom_abline(intercept = total_expenditure / avg_price_misc, slope = -avg_price_dairy / avg_price_misc, color = "red") +
  labs(title = "Indifference Curves and Budget Constraint",
       x = "Dairy",
       y = "Misc",
       color = "Utility") +
  theme_minimal()

# Load necessary libraries
library(readxl)
library(ggplot2)
# Load the grid data for plotting
grid_data <- read_excel("ROBBY_NASHARY_utility_grid_data2.xlsx")
# Define the explicit utility function
explicit_utility_function <- function(U, misc) {
  return((U^2) / misc)
}
# Create a dataframe to store the calculated dairy values
dairy_values <- data.frame()
# Calculate dairy values for multiple utility levels
utility_levels <- unique(grid_data$utility)
for (U in utility_levels) {
  for (misc in unique(grid_data$misc)) {
    dairy <- explicit_utility_function(U, misc)
    dairy_values <- rbind(dairy_values, data.frame(utility = U, dairy = dairy, misc = misc))
  }
}
# Plot the explicit utility function using ggplot2
ggplot(dairy_values, aes(x = dairy, y = misc, color = factor(utility))) +
  geom_line() +
  labs(title = "Explicit Utility Function: Dairy = U^2 / Misc",
       x = "Dairy",
       y = "Misc",
       color = "Utility Level") +
  theme_minimal()

# Load necessary libraries
library(readxl)
library(writexl)
## Warning: package 'writexl' was built under R version 4.3.3
# Load the dataset
data <- read_excel("ROBBY NASHARY - food_consumption.xlsx", sheet = "data")
# Hitung utilitas
data$utility <- sqrt(data$w_dairy * data$w_misc)
# Hitung Marginal Rate of Substitution (MRS)
data$MRS <- -data$w_misc / data$w_dairy
# Simpan hasil ke file baru
write_xlsx(data, "food_consumption_with_utility_and_mrs.xlsx")
# Load necessary libraries
library(readxl)
library(ggplot2)
# Load the dataset
data <- read_excel("ROBBY NASHARY - food_consumption.xlsx", sheet = "data")
# Calculate average values of dairy and misc
avg_dairy <- mean(data$w_dairy, na.rm = TRUE)
avg_misc <- mean(data$w_misc, na.rm = TRUE)
# Create a scatter plot with regression fit line
ggplot(data, aes(x = w_dairy, y = w_misc)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  geom_vline(xintercept = avg_dairy, linetype = "dashed", color = "blue") +
  geom_hline(yintercept = avg_misc, linetype = "dashed", color = "green") +
  labs(title = "Scatter Plot of Dairy vs Misc with Regression Fit Line",
       x = "Dairy",
       y = "Misc") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Load necessary libraries
library(readxl)

# Load the dataset
data <- read_excel("ROBBY NASHARY - food_consumption.xlsx", sheet = "data")
# Hitung nilai rata-rata dairy dan misc
avg_dairy <- mean(data$w_dairy, na.rm = TRUE)
avg_misc <- mean(data$w_misc, na.rm = TRUE)
# Hitung MRS
MRS <- -avg_misc / avg_dairy
# Tampilkan hasil
cat("Average Dairy:", avg_dairy, "\n")
## Average Dairy: 0.1505844
cat("Average Misc:", avg_misc, "\n")
## Average Misc: 0.1069968
cat("MRS:", MRS, "\n")
## MRS: -0.7105434
# Load necessary libraries
library(readxl)
library(ggplot2)

# Load the dataset
data <- read_excel("ROBBY NASHARY - food_consumption.xlsx", sheet = "data")
# Hitung nilai rata-rata
avg_dairy <- mean(data$w_dairy, na.rm = TRUE)
avg_misc <- mean(data$w_misc, na.rm = TRUE)
# Hitung MRS
MRS <- -avg_misc / avg_dairy
# Buat grid untuk kurva indiferen
utility_levels <- seq(0.1, 1, by = 0.1)
grid_data <- expand.grid(dairy = seq(0.01, 0.5, by = 0.01),
                         misc = seq(0.01, 0.5, by = 0.01))
grid_data$utility <- sqrt(grid_data$dairy * grid_data$misc)
# Visualisasi
ggplot(grid_data, aes(x = dairy, y = misc)) +
  stat_contour(aes(z = utility), bins = 10, color = "blue") +
  geom_point(data = data, aes(x = w_dairy, y = w_misc), alpha = 0.3) +
  geom_point(aes(x = avg_dairy, y = avg_misc), color = "red", size = 3) +
  geom_abline(intercept = avg_dairy - MRS * avg_misc, slope = MRS, color = "darkgreen", linetype = "dashed") +
  labs(title = "Convexity and Marginal Rate of Substitution (MRS)",
       x = "Dairy",
       y = "Misc") +
  theme_minimal()
## Warning in geom_point(aes(x = avg_dairy, y = avg_misc), color = "red", size = 3): All aesthetics have length 1, but the data has 2500 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.