### R code from vignette source '2-linearRegression.Rnw' ################################################### ### code chunk number 1: 2-linearRegression.Rnw:20-21 ################################################### options(width=60) ################################################### ### code chunk number 2: dataset ################################################### library(ggplot2) set.seed(123) index <- sample(1:nrow(diamonds), 50) # try a subset first diamonds2 <- diamonds[index,] ################################################### ### code chunk number 3: plot ################################################### library(ggplot2) plot(price ~ carat, data=diamonds2, col="blue") ################################################### ### code chunk number 4: plot ################################################### cor(diamonds2$price, diamonds2$carat) # random 50 cor(diamonds$price, diamonds$carat) # all the diamonds ################################################### ### code chunk number 5: pairwiseCorrelations ################################################### cor(diamonds2[,c('carat','depth','price','x','y','z')]) ################################################### ### code chunk number 6: pairwiseCorrelations1 ################################################### pairs(diamonds2[,c('carat','depth','price','x','y','z')]) ################################################### ### code chunk number 7: pairwiseCorrelations2 ################################################### library(dplyr) diamonds2 %>% select(carat,depth,price,x,y,z) %>% pairs() ################################################### ### code chunk number 8: propertiesOfCorrelation3 ################################################### x <- rnorm(10) y <- rnorm(10) plot(x, y, main=paste('Correlation =', round(cor(x,y), digits=2))) ################################################### ### code chunk number 9: propertiesOfCorrelation4 ################################################### x <- 1:10 y <- 10*x + rnorm(10, sd=4) plot(x, y, main=paste('Correlation =', round(cor(x,y), digits=2))) ################################################### ### code chunk number 10: propertiesOfCorrelation5 ################################################### x <- 1:10 y <- 10*x^4 + rnorm(10, sd=4) plot(x, y, main=paste('Correlation =', round(cor(x,y), digits=2))) ################################################### ### code chunk number 11: propertiesOfCorrelation6 ################################################### x <- c(rnorm(9), 10) y <- c(rnorm(9), 30) plot(x, y, main=paste('Correlation =', round(cor(x,y), digits=2))) ################################################### ### code chunk number 12: propertiesOfCorrelation7 ################################################### plot(price ~ carat, data=diamonds2, col="blue") abline(lm(price ~ carat, data=diamonds2)) ################################################### ### code chunk number 13: propertiesOfCorrelation8 ################################################### summary(lm(price ~ carat, data=diamonds2)) # equivalently # diamonds2 %>% select(price, carat) %>% lm() %>% summary()