library(readr)
housing <- read_csv("~/Desktop/STA 108/housing.csv")
## Rows: 195 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): county
## dbl (5): price, br, lsqft, bsqft, year
## 
## ℹ 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.

To include: scatterplot, histogram, boxplot, summary of value

price vs. br

names(housing)[c(1,2)] = c("Y", "X1")#Renames the first two columnns
the.model = lm(Y ~ X1, data = housing)
par(mfrow=  c(2,2)) #Makes a 2 x 2 grid which plots will fill in to

#scatterplot
plot(housing$X1, housing$Y, main = "Price vs # Bedrooms", ylab = "Price",xlab = "# Bedrooms", pch = 19,font = 2,font.lab = 2,cex = 0.75)

#histogram
hist(housing$X1, main = "Frequency of # Bedrooms", ylab = "Frequency", xlab = "# Bedrooms",pch = 19,font = 2,font.lab = 2,cex = 0.75)

#boxplot
boxplot(housing$X1, main = "Price vs # Bedrooms", ylab = "Price", xlab = "# Bedrooms", horizontal = TRUE, pch = 19,font = 2,font.lab = 2,cex = 0.75) 

abline(h = 0, lwd = 2, col = "purple")

# summary statistics
summary_stats <- data.frame(
  X_bar = mean(housing$X1),
  Y_bar = mean(housing$Y),
  SD_X = sd(housing$X1),
  SD_Y = sd(housing$Y),
  #ask this from prof/DOUBLE CHECK
  Error_Y = sd(housing$Y)/sqrt(nrow(housing))
)

# Print summary statistics table
print(summary_stats)
##      X_bar    Y_bar      SD_X     SD_Y  Error_Y
## 1 2.907692 538.5487 0.8919412 342.4182 24.52108

price vs. lsqft

names(housing)[c(1,3)] = c("Y","X2")#Renames the first two columnns
the.model = lm(Y ~ X2, data = housing)
par(mfrow=  c(2,2)) #Makes a 2 x 2 grid which plots will fill in to

#scatterplot
plot(housing$X2, housing$Y, main = "Price vs # Lot Sqft", ylab = "Price",xlab = "Lot Sqft", pch = 19,font = 2,font.lab = 2,cex = 0.75)

#histogram
hist(housing$X2, main = "Frequency of Lot Sqft ", ylab = "Frequency", xlab = "Lot Sqft",pch = 19,font = 2,font.lab = 2,cex = 0.75)

#boxplot
boxplot(housing$X2, main = "Price vs # Lot Sqft", ylab = "Price", xlab = "Lot Sqft", horizontal = TRUE, pch = 19,font = 2,font.lab = 2,cex = 0.75) 

abline(h = 0, lwd = 2, col = "purple")

# summary statistics
summary_stats <- data.frame(
  X_bar = mean(housing$X2),
  Y_bar = mean(housing$Y),
  SD_X = sd(housing$X2),
  SD_Y = sd(housing$Y),
  Error_Y = sd(housing$Y)/sqrt(nrow(housing))
)

# Print summary statistics table
print(summary_stats)
##      X_bar    Y_bar     SD_X     SD_Y  Error_Y
## 1 25419.28 538.5487 88973.11 342.4182 24.52108

price vs. bsqft

names(housing)[c(1,4)] = c("Y","X3")#Renames the first two columnns
the.model = lm(Y ~ X3, data = housing)
par(mfrow=  c(2,2)) #Makes a 2 x 2 grid which plots will fill in to

#scatterplot
plot(housing$X3, housing$Y, main = "Price vs # House Sqft", ylab = "Price",xlab = "House Sqft", pch = 19,font = 2,font.lab = 2,cex = 0.75)

#histogram
hist(housing$X3, main = "Frequency of House Sqft ", ylab = "Frequency", xlab = "House Sqft",pch = 19,font = 2,font.lab = 2,cex = 0.75)

#boxplot
boxplot(housing$X3, main = "Price vs # House Sqft", ylab = "Price", xlab = "Lot Sqft", horizontal = TRUE, pch = 19,font = 2,font.lab = 2,cex = 0.75) 

abline(h = 0, lwd = 2, col = "purple")

# summary statistics
summary_stats <- data.frame(
  X_bar = mean(housing$X3),
  Y_bar = mean(housing$Y),
  SD_X = sd(housing$X3),
  SD_Y = sd(housing$Y),
  Error_Y = sd(housing$Y)/sqrt(nrow(housing))
)

# Print summary statistics table
print(summary_stats)
##      X_bar    Y_bar     SD_X     SD_Y  Error_Y
## 1 1579.195 538.5487 645.4697 342.4182 24.52108

price vs. year

names(housing)[c(1,5)] = c("Y","X4")#Renames the first two columnns
the.model = lm(Y ~ X4, data = housing)
par(mfrow=  c(2,2)) #Makes a 2 x 2 grid which plots will fill in to

#scatterplot
plot(housing$X4, housing$Y, main = "Price vs Year", ylab = "Price",xlab = "Year", pch = 19,font = 2,font.lab = 2,cex = 0.75)

#histogram
hist(housing$X4, main = "Frequency of Year ", ylab = "Frequency", xlab = "Year",pch = 19,font = 2,font.lab = 2,cex = 0.75)

#boxplot
boxplot(housing$X4, main = "Price vs Year", ylab = "Price", xlab = "Year", horizontal = TRUE, pch = 19,font = 2,font.lab = 2,cex = 0.75) 

abline(h = 0, lwd = 2, col = "purple")

# summary statistics
summary_stats <- data.frame(
  X_bar = mean(housing$X4),
  Y_bar = mean(housing$Y),
  SD_X = sd(housing$X4),
  SD_Y = sd(housing$Y),
  Error_Y = sd(housing$Y)/sqrt(nrow(housing))
)

# Print summary statistics table
print(summary_stats)
##      X_bar    Y_bar     SD_X     SD_Y  Error_Y
## 1 1971.077 538.5487 20.03474 342.4182 24.52108

price vs. county(ask if needed)??

Historagm

names(housing)[c(1,6)] = c("Y","X5")#Renames the first two columnns
the.model = lm(Y ~ X5, data = housing)


# Histograms for each county's prices
par(mfrow = c(1, 3))  # Set the plotting layout to one row with three columns

# Histogram for Solano county
hist(housing$Y[housing$X5 == "Solano"], main = "Solano", xlab = "Price", ylab = "Frequency", col = "skyblue")

# Histogram for Sonoma county
hist(housing$Y[housing$X5 == "Sonoma"], main = "Sonoma", xlab = "Price", ylab = "Frequency", col = "skyblue")

# Histogram for Napa county
hist(housing$Y[housing$X5 == "Napa"], main = "Napa", xlab = "Price", ylab = "Frequency", col = "skyblue")

## Scatterplot(FIX)

# Convert the categorical variable 'County' to a factor
housing$X5 <- factor(housing$X5, levels = c("Solano", "Sonoma", "Napa"))

# Plot scatterplot
plot(housing$X5, housing$Y, 
     main = "Price vs County",
     xlab = "County",
     ylab = "Price",
     col = "blue",
     pch = 19)

# Adding labels for each category
legend("topright",
       legend = levels(housing$X5),
       col = "blue",
       pch = 19,
       title = "County")

## BoxPlot(FIX)

# Convert the categorical variable 'County' to a factor
housing$X5 <- factor(housing$X5, levels = c("Solano", "Sonoma", "Napa"))

# Plot boxplot
boxplot(Y ~ X5, data = housing, 
        main = "Price vs County",
        xlab = "County",
        ylab = "Price",
        col = "blue",
        border = "black")

# Adding a legend for the counties
legend("topright",
       legend = levels(housing$X5),
       fill = "blue",
       title = "County")