This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!requireNamespace("BiocManager", quietly = TRUE)) | |
install.packages("BiocManager") | |
BiocManager::install("pcaMethods") | |
library(pcaMethods) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
winePCAmethods <- pca(wine[,-1], scale = "uv", center = T, | |
nPcs = 2, method = "svd") | |
slplot(winePCAmethods, scoresLoadings = c(T,T), | |
scol = wineClasses) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str(winePCAmethods) # slots are marked with @ | |
winePCAmethods@R2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fit lm using all 14 vars | |
modHousesFull <- lm(MEDV ~ ., data = houses) | |
summary(modHousesFull) # R2 = 0.741 | |
# Compare obs. vs. pred. plots | |
par(mfrow = c(1,2)) | |
plot(houses$MEDV, predict(modHouses), | |
xlab = "Observed MEDV", ylab = "Predicted MEDV", | |
main = "PCR", abline(a = 0, b = 1, col = "red")) | |
plot(houses$MEDV, predict(modHousesFull), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n <- 1:20 | |
den <- dbinom(n, 20, 0.7) | |
plot(den, ylab = "Density", xlab = "Number of successes") | |
sum(den) # = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pnorm(1200,1000,200) # this gives us prob x smaller than 1200eur | |
1-pnorm(1200,1000,200) # this is the one, x greater than 1200eur |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qnorm(1-0.16,1000,200) # = 1198.892 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n <- 1:20 | |
den <- dpois(n, 3) | |
plot(den, xlab = "Outcome", ylab = "Density") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myMeans <- vector() | |
for(i in 1:100){ | |
set.seed(i) | |
myMeans <- c(myMeans, mean(rpois(10,3))) | |
} | |
hist(myMeans, main = NULL, xlab = expression(bar(x))) |