library(wooldridge)
## Warning: package 'wooldridge' was built under R version 4.2.3
library(readr)
## Warning: package 'readr' was built under R version 4.2.3
METRICS_1_ <- read_csv("METRICS (1).csv")
## Rows: 50 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (4): Salary, GPA, Metrics, Sex
##
## ℹ 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.
View(METRICS_1_)
library(readr)
houseprices_1_ <- read_csv("houseprices (1).csv")
## Rows: 1000 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (6): price, sqft, age, Utown, Pool, Fplace
##
## ℹ 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.
View(houseprices_1_)
#PART1
# Display column names
colnames(METRICS_1_) # For the METRICS.csv dataset
## [1] "Salary" "GPA" "Metrics" "Sex"
# Show the first few rows to get an overview
head(METRICS_1_) # For METRICS.csv dataset
## # A tibble: 6 × 4
## Salary GPA Metrics Sex
## <dbl> <dbl> <dbl> <dbl>
## 1 29555 3.77 0 1
## 2 27958 3.4 0 0
## 3 27230 2.74 0 0
## 4 31070 3.88 0 1
## 5 27577 2.7 0 1
## 6 30007 3.36 0 0
# Structure of the data
str(METRICS_1_) # To see data types and column structure
## spc_tbl_ [50 × 4] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Salary : num [1:50] 29555 27958 27230 31070 27577 ...
## $ GPA : num [1:50] 3.77 3.4 2.74 3.88 2.7 3.36 2.57 3.81 2.7 3.29 ...
## $ Metrics: num [1:50] 0 0 0 0 0 0 0 0 0 0 ...
## $ Sex : num [1:50] 1 0 0 1 1 0 0 0 0 1 ...
## - attr(*, "spec")=
## .. cols(
## .. Salary = col_double(),
## .. GPA = col_double(),
## .. Metrics = col_double(),
## .. Sex = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
colnames(houseprices_1_)
## [1] "price" "sqft" "age" "Utown" "Pool" "Fplace"
head(houseprices_1_) # First few rows of the housing dataset
## # A tibble: 6 × 6
## price sqft age Utown Pool Fplace
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 205452 2346 6 0 0 1
## 2 185328 2003 5 0 0 1
## 3 248422 2777 6 0 0 0
## 4 154690 2017 1 0 0 0
## 5 221801 2645 0 0 0 1
## 6 199119 2156 6 0 0 1
str(houseprices_1_) # Structure of the housing dataset
## spc_tbl_ [1,000 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ price : num [1:1000] 205452 185328 248422 154690 221801 ...
## $ sqft : num [1:1000] 2346 2003 2777 2017 2645 ...
## $ age : num [1:1000] 6 5 6 1 0 6 9 0 0 0 ...
## $ Utown : num [1:1000] 0 0 0 0 0 0 0 0 0 0 ...
## $ Pool : num [1:1000] 0 0 0 0 0 0 0 0 1 0 ...
## $ Fplace: num [1:1000] 1 1 0 0 1 1 1 1 0 0 ...
## - attr(*, "spec")=
## .. cols(
## .. price = col_double(),
## .. sqft = col_double(),
## .. age = col_double(),
## .. Utown = col_double(),
## .. Pool = col_double(),
## .. Fplace = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
# Load necessary packages
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ── 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
# Load the data for the first problem
metrics_data <- METRICS_1_
# Estimate a linear regression model of Salary on GPA, Metrics, and Sex
metrics_model <- lm(Salary ~ GPA + Metrics + Sex, data = metrics_data)
# Display the summary of the model to evaluate coefficients and statistical significance
summary(metrics_model)
##
## Call:
## lm(formula = Salary ~ GPA + Metrics + Sex, data = metrics_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3120.7 -826.6 -201.8 656.3 3903.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 24241.7 1090.7 22.226 < 2e-16 ***
## GPA 1657.8 356.4 4.651 2.81e-05 ***
## Metrics 5023.5 460.5 10.910 2.38e-14 ***
## Sex -204.8 419.6 -0.488 0.628
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1446 on 46 degrees of freedom
## Multiple R-squared: 0.7382, Adjusted R-squared: 0.7212
## F-statistic: 43.24 on 3 and 46 DF, p-value: 1.948e-13
# Check if econometrics (Metrics) is statistically significant
# Check the p-value for 'Metrics' in the model summary
# Determine if women make more or less than men based on the 'Sex' coefficient
# Load necessary packages
#PART 2
library(tidyverse)
# housing data
houseprices_data <- houseprices_1_
# Estimate a linear regression model
houseprice_model <- lm(price ~ sqft + age + Utown + Pool + Fplace, data = houseprices_data)
summary(houseprice_model)
##
## Call:
## lm(formula = price ~ sqft + age + Utown + Pool + Fplace, data = houseprices_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -47971 -10411 198 10438 44759
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6911.880 4289.365 1.611 0.107410
## sqft 83.183 1.672 49.759 < 2e-16 ***
## age -192.991 51.567 -3.743 0.000193 ***
## Utown 60196.233 971.531 61.960 < 2e-16 ***
## Pool 4352.570 1205.261 3.611 0.000320 ***
## Fplace 1398.810 976.807 1.432 0.152452
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15330 on 994 degrees of freedom
## Multiple R-squared: 0.8686, Adjusted R-squared: 0.8679
## F-statistic: 1314 on 5 and 994 DF, p-value: < 2.2e-16
library(ggplot2)
# scatter plot to show the relationship between price and square footage, with regression line
ggplot(houseprices_data, aes(x = sqft, y = price)) +
geom_point() + # Scatter plot of sqft vs. price
geom_smooth(method = "lm", se = FALSE, color = "blue") + # Regression line
labs(
title = "Square Footage vs. House Price",
x = "Square Footage",
y = "House Price"
)
## `geom_smooth()` using formula = 'y ~ x'
