Project description

The task for Analysis 4 is divided into four parts:

1.Create a new dataset that has an outcome that has a linear relationship with one continuous predictor and a log-linear relationship with a second continuous predictor (with some random error added to the outcome).

2.Create a scatter plot illustrating the relationship between each predictor and the outcome.

3.Estimate a regression model one regression model that accounts for the non-linear relationship between the two models and one that does not account for the non-linear relationship (i.e. it models the non-linear relationship as a linear relationship).

4.Compare the fit of the two models.

1.Creating the dataset

library(tidyverse)
library(knitr)
library(dplyr)

set.seed(123)

# Generating continuous_pred1
continuous_pred1 <- rnorm(100, mean = 50, sd = 10)

# Generating log-normal distribution for continuous_pred2
log_normal_vals <- rnorm(100, mean = 4, sd = 0.5) # Generating log-normal valuesfi
continuous_pred2 <- exp(log_normal_vals) # Transforming back to original scale

# Generating outcome variables
linear_relationship <- 2 * continuous_pred1 + rnorm(100, mean = 0, sd = 10)
log_linear_relationship <- 3 * log(continuous_pred2) + rnorm(100, mean = 0, sd = 5)

# Creating the dataset
data <- data.frame(continuous_pred1, continuous_pred2, linear_relationship, log_linear_relationship)

2.Creating Scatter Plots

  data |>
    ggplot(aes(x = continuous_pred1, y = linear_relationship)) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    labs(x = "Continuous Predictor 1", y = "Outcome (Linear Relationship)", title = "Linear Relationship")
## `geom_smooth()` using formula = 'y ~ x'

  data |>
    ggplot(aes(x = continuous_pred2, y = log_linear_relationship)) +
    geom_point() +
    labs(x = "Continuous Predictor 2", y = "Outcome (Log-Linear Relationship)", title = "Log-Linear Relationship")

3.Fitting Regression Models

  model_nonlinear <- lm(log_linear_relationship ~ log(continuous_pred2) + continuous_pred1, data = data)
  
  model_linear <- lm(log_linear_relationship ~ continuous_pred2 + continuous_pred1, data = data)

4.Comparing Model Fit

  summary(model_nonlinear)
## 
## Call:
## lm(formula = log_linear_relationship ~ log(continuous_pred2) + 
##     continuous_pred1, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.3836  -3.3643   0.4919   3.5338  13.1283 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)   
## (Intercept)           -0.73425    5.35433  -0.137  0.89121   
## log(continuous_pred2)  3.44852    1.09006   3.164  0.00208 **
## continuous_pred1      -0.02390    0.05774  -0.414  0.67978   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.238 on 97 degrees of freedom
## Multiple R-squared:  0.09629,    Adjusted R-squared:  0.07765 
## F-statistic: 5.167 on 2 and 97 DF,  p-value: 0.00737
  summary(model_linear)
## 
## Call:
## lm(formula = log_linear_relationship ~ continuous_pred2 + continuous_pred1, 
##     data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.0077  -3.2770   0.2724   3.4739  13.3298 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       9.51263    3.16137   3.009 0.003339 ** 
## continuous_pred2  0.04951    0.01445   3.426 0.000899 ***
## continuous_pred1 -0.01511    0.05744  -0.263 0.793015    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.196 on 97 degrees of freedom
## Multiple R-squared:  0.1107, Adjusted R-squared:  0.09234 
## F-statistic: 6.036 on 2 and 97 DF,  p-value: 0.003384
  "The Result shows that the non-linear model has higher R-squared value which means has better representation for the data set. Also the p-value is relatively lower, although still not significant statistically."
## [1] "The Result shows that the non-linear model has higher R-squared value which means has better representation for the data set. Also the p-value is relatively lower, although still not significant statistically."