<jupyter_start><jupyter_text>Stats216v: Statistical LearningStanford UniversitySummer 2017<NAME> (<EMAIL>)  3. Linear Regression<jupyter_code>LoadLibraries = function() {
    library(MASS)
    install.packages("ISLR")
    library(ISLR)
    print("Libraries have been loaded!")
}

LoadLibraries()
names(Boston)
# lm(y~x,data)
# y: response, x: predictor
lm.fit = lm(medv~lstat, data=Boston)
summary(lm.fit)
names(lm.fit)
coef(lm.fit)
confint(lm.fit)
par(mfrow=c(2,2))
plot(lm.fit)
# plot(lm.fit$residuals, lm.fit$fitted)
plot(Boston$lstat, Boston$medv)
abline(lm.fit, col="red")
# multiple linear regression
summary(lm(medv~lstat+age, data=Boston))
summary(lm(medv~., data=Boston))
# interaction terms
summary(lm(medv~lstat*age, data=Boston))
# non-linear transformation
summary(lm(medv~lstat+poly(lstat,2), data=Boston))
lm.fit1 = lm(medv~lstat, data=Boston)
lm.fit2 = lm(medv~lstat+poly(lstat,2), data=Boston)
anova(lm.fit1, lm.fit2)

par(mfrow=c(2,2))
plot(lm.fit2)
head(Carseats)
lm.fit = lm(Sales~.+Income:Advertising+Price:Age, data=Carseats)
summary(lm.fit)
contrasts(Carseats$ShelveLoc)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1682991985752449, 'fraction_numerical': 0.016918967052537846, 'mean_word_length': 8.138211382113822, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '227639', 'license': 'MIT', 'n_tokens_mistral': 494, 'n_tokens_neox': 462, 'n_words': 70, 'path': 'notebooks/stats216v/chapter-03-r.ipynb', 'repo_name': 'gyuho/dplearn', 'size': '1136'}