2025-10-30

Slide 1: Introduction

Slide 2: About the Dataset

The mtcars dataset contains data about fuel consumption and car design specifications for 32 vehicles.

# Display the first few rows of the dataset
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
model <- lm(mpg ~ wt + hp, data = mtcars)
summary(model)$coefficients
##                Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 37.22727012 1.59878754 23.284689 2.565459e-20
## wt          -3.87783074 0.63273349 -6.128695 1.119647e-06
## hp          -0.03177295 0.00902971 -3.518712 1.451229e-03
library(shiny)
## Warning: package 'shiny' was built under R version 4.4.3
model <- lm(mpg ~ wt + hp, data = mtcars)

shinyApp(
ui = fluidPage(
titlePanel("MTCARS Predictor Demo"),
sidebarLayout(
sidebarPanel(
sliderInput("wt", "Weight (1000 lbs):", min = 1, max = 6, value = 3),
sliderInput("hp", "Horsepower:", min = 50, max = 350, value = 100)
),
mainPanel(
textOutput("pred")
)
)
),
server = function(input, output) {
output$pred <- renderText({
pred <- predict(model,
newdata = data.frame(wt = input$wt, hp = input$hp))
paste("Predicted MPG =", round(pred, 2))
})
}
)
Shiny applications not supported in static R Markdown documents
predict(model, newdata = data.frame(wt = 3, hp = 100))
##        1 
## 22.41648