homework3_4322
doc
keyboard_arrow_up
School
University of Houston *
*We aren’t endorsed by this school
Course
4322
Subject
Mathematics
Date
Feb 20, 2024
Type
doc
Pages
5
Uploaded by hongyumei411
Homework 3 - MATH 4322
Instructions
1.
Due date: September 28, 2023
2.
Scan or Type your answers and submit only one file. (If you submit several files only the recent one uploaded will be graded).
3.
Preferably save your file as PDF before uploading. Submit in Canvas under Homework 3.
4.
These questions are from An Introduction to Statistical Learning with Applications in R
by James, et. al., chapter 4.
Problem 1
(a)
Estimate the probability that a student who studies for 40 h and has an undergrad GPA of 3.5 gets an A in the class.
t = -6+0.05*40+1*3.5 = -0.5
Y = e^-0.5/(1+e^-0.5) = 0.3775407
37.75% of students receiving an A in class (b)
How many hours would the student in part (a) need to study to have a 50% chance of
getting an A in the class?
0.05*X1+1*3.5-6 = 0 -> 0.05*X1-2.5 = 0 -> 0.05*X1 = 2.5 -> X1 = 50 hours
Problem 2
In this problem, you will develop a model to predict whether a given car gets high or low gas mileage based on the
Auto
data set in the
ISLR
package.
(a)
Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame()
function to create a single data set containing both mpg01 and the other Auto variables.
(b)
Explore the data graphically in order to investigate the association between mpg01 and
the other features. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and boxplots may be useful tools to answer this question. Describe your findings.
Cylinders, weight, displacement, horsepower, and mpgare useful in predicting mpg01
(c)
Split the data into a training set and a test set.
> set.seed(100)
> train <- sample(1:dim(Auto)[1], dim(Auto)[1]*.7, rep=FALSE)
> test <- -train
> training_data<- Auto[train, ]
> testing_data= Auto[test, ]
> mpg01.test <- mpg01[test]
(d)
Perform logistic regression on the training data in order to predict mpg01 using the
variables that seemed most associated with mpg01 in (b). What is the test error of the
model obtained? That is use the test data to predict and get the confusion matrix and
determine the error rate.
> glm_model <- glm(mpg01 ~ cylinders + weight + displacement + horsepower, data = training_data, family = binomial)
> summary(glm_model)
> probs <- predict(glm_model, testing_data, type = "response")
> pred.glm <- rep(0, length(probs))
> pred.glm[probs > 0.5] <- 1
> mean(pred.glm != mpg01.test)
[1] 0.1101695
Test error rate = 11.02%
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Problem 3
This problem involves writing functions.
(a)
Write a function,
Power()
, that prints out the result of raising 2 to the 3rd power. In other words, your function should compute 2
3
and print out the results.
> power = function(a=2,b=3) print(a^b)
> power()
[1] 8
(b)
Create a new function,
Power2()
, that allows you to pass any two numbers, and ,
and prints out the value of . You can do this by beginning your function with the line
Power2 <- function(x, a) {
You should be able to call your function by entering, for instance,
Power2(3, 8)
on the command line. This should output the value of 3
8
, namely, 6,561.
> power2 <- function(x, a) {
+ result = x^a
+ print(result)
+ }
> power2(3,8)
[1] 6561
(c)
Using the
Power2()
function that you just wrote, compute 10
3
, 8
17
, and 131
3
.
> power2(10,3)
[1] 1000
> power2(8,17)
[1] 2.2518e+15
> power2(131,3)
[1] 2248091
(d)
Now create a new function,
Power3()
, that actually returns the result as an
R
object, rather than simply printing it to the screen. That is, if you store the value in an
object called
result
within your function, then you can simply
return()
this
result, using the following line:
return(result)
The line above should be the last line in your function, before the } symbol.
> power3 = function(x,a){
+ result = x^a
+ return (result)
+ }
(e)
Now using the
Power3()
function, create a plot of f(x)=x
2
. The x-axis should display
a range of integers from 1 to 10, and the y-axis should display x
2
. Label the axes
appropriately, and use an appropriate title for the figure.
(f)
Create a function,
PlotPower()
, that allows you to create a plot of against for a fixed and for a range of values of . For instance, if you call
> PlotPower = function(x,a){
+ y = power3(x,a)
+ plot(x,y,xlab="x",ylab="y",main=paste("y=x^",a))
+ }