7.1
Q: After reading the dataset into R (naming it using your student ID as usual), identify the number of observations, data types, as well as the mean, median, and quartiles for each variable in the dataset. Use any function(s) that you like. Show your function(s) and your output.
Consider other statistics or graphics you may want to create.
setwd("C:\\Users\\youse\\Downloads")
data03 <- read.csv("CommercialProperties.csv")
str(data03)
## 'data.frame': 82 obs. of 5 variables:
## $ Rental.Rates : num 13.5 12 10.5 15 14 10.5 14 16.5 17.5 16.5 ...
## $ Age : int 1 14 16 4 11 15 2 1 1 8 ...
## $ Operating.Expenses.and.Taxes: num 5.02 8.19 3 10.7 8.97 ...
## $ Vacancy.Rates : num 0.14 0.27 0 0.05 0.07 0.24 0.19 0.6 0 0.03 ...
## $ Total.Square.Footage : int 123000 104079 39998 57112 60000 101385 31300 248172 215000 251015 ...
##Obs. Numbers#
num_observations <- nrow(data03)
cat("Number of observations:", num_observations, "\n")
## Number of observations: 82
# Data types
data_types <- sapply(data03, class)
print(data_types)
## Rental.Rates Age
## "numeric" "integer"
## Operating.Expenses.and.Taxes Vacancy.Rates
## "numeric" "numeric"
## Total.Square.Footage
## "integer"
# Mean, median, and quartiles for each variable
summary(data03)
## Rental.Rates Age Operating.Expenses.and.Taxes Vacancy.Rates
## Min. :10.5 Min. : 0.000 Min. : 3.000 Min. :0.00000
## 1st Qu.:14.0 1st Qu.: 2.000 1st Qu.: 8.145 1st Qu.:0.00000
## Median :15.0 Median : 4.000 Median :10.370 Median :0.03000
## Mean :15.2 Mean : 8.012 Mean : 9.814 Mean :0.08244
## 3rd Qu.:16.5 3rd Qu.:15.000 3rd Qu.:11.620 3rd Qu.:0.09750
## Max. :20.0 Max. :20.000 Max. :20.000 Max. :0.73000
## Total.Square.Footage
## Min. : 27000
## 1st Qu.: 70500
## Median :129614
## Mean :164772
## 3rd Qu.:239000
## Max. :500020
mean_median_quartiles <- sapply(data03, function(x) c(mean = mean(x, na.rm = TRUE),
median = median(x, na.rm = TRUE),
Q1 = quantile(x, 0.25, na.rm = TRUE),
Q3 = quantile(x, 0.75, na.rm = TRUE)))
print(mean_median_quartiles)
## Rental.Rates Age Operating.Expenses.and.Taxes Vacancy.Rates
## mean 15.19817 8.012195 9.813902 0.08243902
## median 15.00000 4.000000 10.370000 0.03000000
## Q1.25% 14.00000 2.000000 8.145000 0.00000000
## Q3.75% 16.50000 15.000000 11.620000 0.09750000
## Total.Square.Footage
## mean 164772.1
## median 129614.0
## Q1.25% 70500.0
## Q3.75% 239000.0
hist(data03$Rental.Rates, main="Distribution of Rental Rates", xlab="Rental Rates
($USD per square foot)", col="skyblue", border="black")
7.2
Q: Perform a series of regressions where you predict 7.2.1 rental rates of the properties (Rental Rates) using Age, 7.2.2 rental rates of the properties (Rental Rates) using Operating Expenses & Taxes, 7.2.3 rental rates of the properties (Rental Rates) using Vacancy Rates, and 7.2.4 rental rates of the properties (Rental Rates) using Total Square Footage Please run four simple linear regressions. Show your commands and your output. After running these 4 models, which independent variables (IVs) seem to have a statistically-significant effect on the dependent variable (DV) of Rental Rates?
##7.2#
##7.2.1#
model_age <- lm(Rental.Rates ~ Age, data = data03)
summary(model_age)
##
## Call:
## lm(formula = Rental.Rates ~ Age, data = data03)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.3739 -0.9308 0.1209 1.0112 5.3582
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 15.57003 0.30593 50.895 <2e-16 ***
## Age -0.04641 0.02932 -1.583 0.117
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.775 on 80 degrees of freedom
## Multiple R-squared: 0.03038, Adjusted R-squared: 0.01826
## F-statistic: 2.506 on 1 and 80 DF, p-value: 0.1173
##7.2.2#
model_expenses <- lm(Rental.Rates ~ Operating.Expenses.and.Taxes, data = data03)
summary(model_expenses)
##
## Call:
## lm(formula = Rental.Rates ~ Operating.Expenses.and.Taxes, data = data03)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5862 -0.9241 -0.1766 0.7576 4.8161
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.17872 0.63735 19.109 < 2e-16 ***
## Operating.Expenses.and.Taxes 0.30767 0.06247 4.925 4.45e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.579 on 80 degrees of freedom
## Multiple R-squared: 0.2327, Adjusted R-squared: 0.2231
## F-statistic: 24.26 on 1 and 80 DF, p-value: 4.452e-06
##7.2.3#
model_vacancy <- lm(Rental.Rates ~ Vacancy.Rates, data = data03)
summary(model_vacancy)
##
## Call:
## lm(formula = Rental.Rates ~ Vacancy.Rates, data = data03)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8924 -1.1243 -0.0965 1.1264 4.6569
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 15.0965 0.2329 64.815 <2e-16 ***
## Vacancy.Rates 1.2329 1.4841 0.831 0.409
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.795 on 80 degrees of freedom
## Multiple R-squared: 0.008552, Adjusted R-squared: -0.003841
## F-statistic: 0.6901 on 1 and 80 DF, p-value: 0.4086
##7.2.4#
model_sqft <- lm(Rental.Rates ~ Total.Square.Footage, data = data03)
summary(model_sqft)
##
## Call:
## lm(formula = Rental.Rates ~ Total.Square.Footage, data = data03)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.1236 -0.7636 0.2848 1.0718 3.3699
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.370e+01 2.848e-01 48.117 < 2e-16 ***
## Total.Square.Footage 9.065e-06 1.421e-06 6.377 1.08e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.468 on 80 degrees of freedom
## Multiple R-squared: 0.337, Adjusted R-squared: 0.3287
## F-statistic: 40.67 on 1 and 80 DF, p-value: 1.084e-08
##The Age of the property, Operating Expenses & Taxes, and Total Square Footage exhibit statistically significant associations with Rental Rates. This suggests that changes in these variables are linked to changes in Rental Rates.However, Vacancy Rates do not demonstrate a statistically significant association with Rental Rates.#
##7.3#
Q: Look at your output for the regression of rental rates and total square footage. How many observations are analyzed (what is the “n” value)? What is the regression equation? What is the R2 value? [use the “Multiple R-Squared”]. What percentage of variation does this model explain?
According to this model, how much would a property of 300,000 square feet rent for?
Answer:
Number of observations (n value):
From the regression output, the degrees of freedom for the residuals is mentioned as 79. This means the total number of observations (n value) is 79 + 3 = 82, where 3 represents the degrees of freedom used by the three parameters (intercept and the two coefficients).
Regression equation:
The regression equation is given by:
\[ \text{Rental Rates} = B_0 + B_1 \times \text{Total Square Footage} \]
Where: - \(B_0\) (Intercept) = 13.70 - \(B_1\) (Coefficient of Total Square Footage) = \(9.065 \times 10^{-6}\)
Hence, the equation is:
\[ \text{Rental Rates} = 13.70 + 9.065 \times 10^{-6} \times \text{Total Square Footage} \]
R² value (Multiple R-Squared):
The R² value, or Multiple R-Squared, indicates the proportion of the variance in the dependent variable (rental rates) that is explained by the independent variable (total square footage). From the output, the Multiple R-Squared value is approximately 0.337.
Percentage of variation explained:
The R² value of 0.337 means that this model explains approximately 33.7% of the variation in rental rates.
Rental prediction for a property of 300,000 square feet:
To predict the rental rate for a property of 300,000 square feet, we can use the regression equation. Substituting the value of 300,000 for Total Square Footage:
\[ \text{Rental Rates} = 13.70 + 9.065 \times 10^{-6} \times 300,000 \]
\[ \text{Rental Rates} \approx 13.70 + 2.72 \]
\[ \text{Rental Rates} \approx 16.42 \]
According to this model, a property of 300,000 square feet would rent for approximately $16.42 per square foot.
##7.4#
## Q: Use the ggplot() to check the required conditions for the model [only do model 4, the one with total square footage as the independent variable and rental rates as the dependent variable]. Show your commands and your output. Choose your own colors for your graphics. Be sure you have appropriate titles on all of your graphics, and that all axes are labeled appropriately. State your conclusion about the usefulness of the model.#
library(ggplot2)
# Compute leverage values
data03$leverage <- hatvalues(model_sqft)
# Fit a linear regression model
model_sqft <- lm(Rental.Rates ~ Total.Square.Footage, data = data03)
# Check if residuals exist
if (!"residuals" %in% colnames(data03)) {
# If residuals column does not exist, compute residuals
data03$residuals <- residuals(model_sqft)
}
# Check if fitted.values exist
if (!"fitted.values" %in% colnames(data03)) {
# If fitted.values column does not exist, add it to the dataset
data03$fitted.values <- fitted(model_sqft)
}
# Add fitted values to the dataset
data03$fitted.values <- fitted(model_sqft)
# Plot 1: Residuals vs. Fitted values
p1 <- ggplot(data03, aes(x = fitted.values, y = residuals)) +
geom_point(color = "darkblue") +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(title = "Residuals vs. Fitted Values", x = "Fitted Values", y = "Residuals") +
theme(plot.title = element_text(hjust = 0.5)) # Centering the title
# Print Plot 1
print(p1)
# Plot 2: Histogram of the residuals
p2 <- ggplot(data03, aes(x = residuals)) +
geom_histogram(fill = "green", color = "black", binwidth = 0.5) +
labs(title = "Histogram of Residuals", x = "Residuals", y = "Frequency") +
theme(plot.title = element_text(hjust = 0.5)) # Centering the title
# Print Plot 2
print(p2)
# Plot 3: QQ plot of the residuals
p3 <- ggplot(data03, aes(sample = residuals)) +
geom_qq(color = "red") +
geom_qq_line(color = "blue") +
labs(title = "QQ Plot of Residuals") +
theme(plot.title = element_text(hjust = 0.5)) # Centering the title
# Print Plot 3
print(p3)
# Plot 4: Residuals vs. Leverage
p4 <- ggplot(data03, aes(x = leverage, y = residuals)) +
geom_point(color = "pink") +
labs(title = "Residuals vs. Leverage", x = "Leverage", y = "Residuals") +
theme(plot.title = element_text(hjust = 0.5)) # Centering the title
# Print Plot 4
print(p4)