Skip to content

Instantly share code, notes, and snippets.

View monogenea's full-sized avatar

Francisco Lima monogenea

View GitHub Profile
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("pcaMethods")
library(pcaMethods)
winePCAmethods <- pca(wine[,-1], scale = "uv", center = T,
nPcs = 2, method = "svd")
slplot(winePCAmethods, scoresLoadings = c(T,T),
scol = wineClasses)
str(winePCAmethods) # slots are marked with @
winePCAmethods@R2
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 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),
n <- 1:20
den <- dbinom(n, 20, 0.7)
plot(den, ylab = "Density", xlab = "Number of successes")
sum(den) # = 1
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
qnorm(1-0.16,1000,200) # = 1198.892
n <- 1:20
den <- dpois(n, 3)
plot(den, xlab = "Outcome", ylab = "Density")
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)))