HW_9
Rmd
keyboard_arrow_up
School
University of Wisconsin, Madison *
*We aren’t endorsed by this school
Course
1203
Subject
Industrial Engineering
Date
Dec 6, 2023
Type
Rmd
Pages
2
Uploaded by PresidentMonkey1089
---
title: "HW 9"
author: "Junyu Sui"
output:
pdf_document:
number_sections: true
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE,
warnings = FALSE, fig.align = 'center',
eval = TRUE)
```
You can run the following code to prepare the analysis.
```{r}
library(r02pro) #INSTALL IF NECESSARY
library(tidyverse) #INSTALL IF NECESSARY
library(MASS)
library(corrplot)
library(FactoMineR)
library("factoextra")
my_ahp <- ahp %>% dplyr::select(gar_car, liv_area, lot_area, bsmt_area, gar_area,
oa_qual, sale_price, bedroom, bathroom, yr_built) %>%
na.omit()
my_ahp_x <- my_ahp %>% dplyr::select(-sale_price)
my_ahp_y <- my_ahp %>% dplyr::select(sale_price)
```
1. Conduct PCA on `my_ahp_x` with `scale = TRUE`.
a. Create a biplot.
```{r}
pr.ahp <- prcomp(my_ahp_x, scale = TRUE)
biplot(pr.ahp, scale = 0)
```
b. Plot the Proportion of Variance Explained and the Cumulative Proportion of
Variance Explained.
```{r}
pr.var <- pr.ahp$sdev^2
pve <- pr.var / sum(pr.var)
par(mfrow = c(1, 2))
# Proportion of variance explained by each of the four components
plot(pve, xlab = "Principal Component",
ylab = "Proportion of Variance Explained", ylim = c(0, 1),
type = "b")
# cumulative proportion of variance explained by the four compoents
plot(cumsum(pve), xlab = "Principal Component",
ylab = "Cumulative Proportion of Variance Explained", ylim = c(0, 1), type = "b")
```
c. Fit a linear regression of `sale_price` on the first two principle components.
What's the $R^2$?
```{r}
fviz_cos2(pr.ahp, choice = "var", axes = 1:2)
fit1<- lm(sale_price ~ pr.ahp$x[,1]+pr.ahp$x[,2 ], data = my_ahp )
summary(fit1)
```
d. Fit a linear regression of `sale_price` on `gar_car` and `liv_area`. What's the
$R^2$?
```{r}
fit2<- lm(sale_price ~ gar_car+liv_area, data = my_ahp )
summary(fit2)
```
\newpage
2. Conduct PCA on `my_ahp_x` with `scale = FALSE` and compare the results of a-c
with those of Q1.
a. Create a biplot.
```{r,warning=FALSE}
pr.ahp1 <- prcomp(my_ahp_x, scale = FALSE)
biplot(pr.ahp1, scale = 0)
```
b. Plot the Proportion of Variance Explained and the Cumulative Proportion of
Variance Explained.
```{r}
pr.var1 <- pr.ahp1$sdev^2
pve1 <- pr.var1 / sum(pr.var1)
par(mfrow = c(1, 2))
# Proportion of variance explained by each of the four components
plot(pve1, xlab = "Principal Component",
ylab = "Proportion of Variance Explained", ylim = c(0, 1),
type = "b")
# cumulative proportion of variance explained by the four compoents
plot(cumsum(pve1), xlab = "Principal Component",
ylab = "Cumulative Proportion of Variance Explained", ylim = c(0, 1), type = "b")
```
c. Fit a linear regression of `sale_price` on the first two principle components.
What's the $R^2$?
```{r}
fviz_cos2(pr.ahp1, choice = "var", axes = 1:2)
fit3 <- lm(sale_price ~ pr.ahp1$x[,1]+pr.ahp1$x[,2], data = my_ahp)
summary(fit3)
```
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help