launch <- read.csv("challenger.csv")
View(launch)
# estimate beta manually
b <- cov(launch$temperature, launch$distress_ct) / var(launch$temperature)
b
# estimate alpha manually
a <- mean(launch$distress_ct) - b * mean(launch$temperature)
a
r <- cov(launch$temperature, launch$distress_ct) /
(sd(launch$temperature) * sd(launch$distress_ct))
r
cor(launch$temperature, launch$distress_ct)
r * (sd(launch$distress_ct) / sd(launch$temperature))
model <- lm(distress_ct ~ temperature, data = launch)
model
summary(model)
# creating a simple multiple regression function
reg <- function(y, x) {
x <- as.matrix(x)
x <- cbind(Intercept = 1, x)
b <- solve(t(x) %*% x) %*% t(x) %*% y
colnames(b) <- "estimate"
print(b)
}
# creating a simple multiple regression function
reg <- function(y, x) {
x <- as.matrix(x)
x <- cbind(Intercept = 1, x)
b <- solve(t(x) %*% x) %*% t(x) %*% y
colnames(b) <- "estimate"
print(b)
}
# examine the launch data
str(launch)
# test regression model with simple linear regression
reg(y = launch$distress_ct, x = launch[2])
# use regression model with multiple regression
reg(y = launch$distress_ct, x = launch[2:4])
model <- lm(distress_ct ~ temperature + pressure + launch_id, data = launch)
# confirming the multiple regression result using the lm function (not in text)
# note that l
model <- lm(distress_ct ~ temperature + pressure + flight_num, data = launch)
# confirming the multiple regression result using the lm function (not in text)
# note that l
model <- lm(distress_ct ~ temperature + field_check_pressure + flight_num, data = launch)
model
summary(model)
model <- lm(distress_ct ~ temperature + field_check_pressure , data = launch)
summary(model)
## Step 2: Exploring and preparing the data ----
insurance <- read.csv("insurance.csv", stringsAsFactors = TRUE)
str(insurance)
# summarize the charges variable
summary(insurance$expenses)
# histogram of insurance charges
hist(insurance$expenses)
# table of region
table(insurance$region)
# exploring relationships among features: correlation matrix
cor(insurance[c("age", "bmi", "children", "expenses")])
# visualing relationships among features: scatterplot matrix
pairs(insurance[c("age", "bmi", "children", "expenses")])
# more informative scatterplot matrix
library(psych)
pairs.panels(insurance[c("age", "bmi", "children", "expenses")])
# set up training and test data sets
indx <- sample(1:nrow(insurance), as.integer(0.9*nrow(insurance)))
indx
insurance_train <- insurance[indx,]
insurance_test <- insurance[-indx,]
## Step 3: Training a model on the data ----
ins_model <- lm(expenses ~ age + children + bmi + sex + smoker + region,
data = insurance_train)
ins_model <- lm(expenses ~ ., data = insurance_train) # this is equivalent to above
# see the estimated beta coefficients
ins_model
# generate predictions for the testing dataset
ins_pred <- predict(ins_model, insurance_test)
# compare the distribution of predicted values vs. actual values
summary(ins_pred)
summary(insurance_test_vals)
# mean absolute error between predicted and actual values
MAE(ins_pred, insurance_test)
# function to calculate the mean absolute error
MAE <- function(actual, predicted) {
mean(abs(actual - predicted))
}
# mean absolute error between predicted and actual values
MAE(ins_pred, insurance_test)
summary(ins_model)
# generate predictions for the testing dataset
ins_pred <- predict(ins_model, insurance_test)
# compare the distribution of predicted values vs. actual values
summary(ins_pred)
summary(insurance_test_vals)
summary(insurance_test)
# plot
plot(ins_pred, insurance_test$expenses)
# compare the correlation
cor(ins_pred, insurance_test$expenses)
# function to calculate the mean absolute error
MAE <- function(actual, predicted) {
mean(abs(actual - predicted))
}
# mean absolute error between predicted and actual values
MAE(ins_pred, insurance_test)
insurance_test
# mean absolute error between predicted and actual values
MAE(ins_pred, insurance_test$expenses)
# add a higher-order "age" term
insurance_train$age2 <- insurance_train$age^2
# add an indicator for BMI >= 30
insurance_train$bmi30 <- ifelse(insurance_train$bmi >= 30, 1, 0)
View(insurance_train)
# create final model
ins_model2 <- lm(expenses ~ age + age2 + children + bmi + sex +
bmi30*smoker + region, data = insurance_train)
summary(ins_model2)
