R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot. #Question 1 # Load dataset data(mtcars)

Fit linear model

model1 <- lm(mpg ~ hp, data = mtcars)

Scatterplot with regression line

plot(mtcars\(hp, mtcars\)mpg, main = “MPG vs Horsepower”, xlab = “Horsepower”, ylab = “Miles Per Gallon”, pch = 19) abline(model1, col = “blue”, lwd = 2)

Regression equation

summary(model1)

#Question2 # Correlation matrix vars <- mtcars[, c(“mpg”, “wt”, “hp”)] cor(vars)

Fit model

model2 <- lm(mpg ~ wt + hp, data = mtcars) summary(model2)

Residual plots

par(mfrow = c(2, 2)) plot(model2)

#Question3 # Simulate data set.seed(42) x <- seq(1, 10, by=0.5) y <- 2 * exp(0.3 * x) + rnorm(length(x), mean=0, sd=3)

Plot raw data

plot(x, y, main = “Non-linear Curve Fit”, xlab = “x”, ylab = “y”, pch = 19)

Fit non-linear model

model3 <- nls(y ~ a * exp(b * x), start = list(a = 1, b = 0.1))

Estimated parameters

coef(model3)

Overlay fitted curve

curve(predict(model3, list(x = x)), add = TRUE, col = “red”, lwd = 2)

Residual plot

residuals_nls <- resid(model3) plot(x, residuals_nls, main = “Residuals of Non-linear Model”, ylab = “Residuals”) abline(h = 0, col = “blue”, lty = 2)