Skip to content

Instantly share code, notes, and snippets.

@monogenea
Created October 7, 2019 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monogenea/008024dbe281887268af50085c82a16a to your computer and use it in GitHub Desktop.
Save monogenea/008024dbe281887268af50085c82a16a to your computer and use it in GitHub Desktop.
houses <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data",
header = F, na.string = "?")
colnames(houses) <- c("CRIM", "ZN", "INDUS","CHAS",
"NOX","RM","AGE","DIS","RAD",
"TAX","PTRATIO","B","LSTAT","MEDV")
# Perform PCA
pcaHouses <- prcomp(scale(houses[,-14]))
scoresHouses <- pcaHouses$x
# Fit lm using the first 3 PCs
modHouses <- lm(houses$MEDV ~ scoresHouses[,1:3])
summary(modHouses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment