library(haven)
curvreg_data <- read_sav("~/Desktop/curvilinear_anxiety.sav")
head(curvreg_data)
## # A tibble: 6 x 8
## anxiety perform anxiety_squared anxiety_third anxiety_mean
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 12 1 1 4
## 2 1 15 1 1 4
## 3 2 26 4 8 4
## 4 2 22 4 8 4
## 5 3 42 9 27 4
## 6 3 36 9 27 4
## # … with 3 more variables: anxiety_centered <dbl>,
## # anxiety_centered_squared <dbl>, anxiety_centered_third <dbl>
# Plot a Scatterplot between IV and DV
plot(curvreg_data$anxiety,curvreg_data$perform, main="Scatterplot betwwen Anxiety and Performance",xlab="Anxiety",ylab="Performance")
# Section 2. Run the Curvilinear Regression Model ## Linear regression model
# Run a linear regression model by using lm() function
curvreg_lm <- lm(perform~anxiety,data=curvreg_data)
summary(curvreg_lm)
##
## Call:
## lm(formula = perform ~ anxiety, data = curvreg_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -17.196 -12.710 -3.429 13.277 20.161
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.7857 8.5704 3.475 0.00458 **
## anxiety -0.5893 1.9164 -0.307 0.76374
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 14.34 on 12 degrees of freedom
## Multiple R-squared: 0.007818, Adjusted R-squared: -0.07486
## F-statistic: 0.09455 on 1 and 12 DF, p-value: 0.7637
# Add fit lines
plot(perform~anxiety,data=curvreg_data)
abline(lm(perform~anxiety,data=curvreg_data))
## Quadratic regression model
# This is pretty similar with the multiple regression. The second independent variable here is the squared value of the 1st variable.
# Run a Quadratic regression model by using lm() function
curvreg_Quadm <- lm(perform~anxiety+anxiety_squared,data=curvreg_data)
summary(curvreg_Quadm)
##
## Call:
## lm(formula = perform ~ anxiety + anxiety_squared, data = curvreg_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.2500 -2.7946 0.5952 2.9405 9.3214
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -13.5714 5.5081 -2.464 0.0315 *
## anxiety 28.3155 3.1566 8.970 2.17e-06 ***
## anxiety_squared -3.6131 0.3856 -9.369 1.41e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.998 on 11 degrees of freedom
## Multiple R-squared: 0.8895, Adjusted R-squared: 0.8694
## F-statistic: 44.28 on 2 and 11 DF, p-value: 5.473e-06
# load the ggplot package to use ggplot() function
library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
# Add fit lines
ggplot(curvreg_data, aes(x=anxiety, y=perform)) + geom_point()+stat_smooth(se=F, method='lm', formula=y~poly(x,2))
## Cubic regression model
# This is still pretty similar with the multiple regression. The third independent variable here is the cubic value of the 1st variable.
# Run a Quadratic regression model by using lm() function
curvreg_cubreg <- lm(perform~anxiety+anxiety_squared+anxiety_third,data=curvreg_data)
summary(curvreg_cubreg)
##
## Call:
## lm(formula = perform ~ anxiety + anxiety_squared + anxiety_third,
## data = curvreg_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.8333 -2.2113 0.2024 3.0863 8.7381
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -10.07143 10.68184 -0.943 0.3680
## anxiety 24.32937 10.77739 2.257 0.0476 *
## anxiety_squared -2.44643 3.03087 -0.807 0.4383
## anxiety_third -0.09722 0.25035 -0.388 0.7059
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.203 on 10 degrees of freedom
## Multiple R-squared: 0.8912, Adjusted R-squared: 0.8585
## F-statistic: 27.29 on 3 and 10 DF, p-value: 3.944e-05
# load the ggplot package to use ggplot() function
# Add fit lines
ggplot(curvreg_data, aes(x=anxiety, y=perform)) + geom_point()+stat_smooth(se=F, method='lm', formula=y~poly(x,3))
# Section 3. Model Comparison
# From the previous section, we got three regression models, namely, linear, quadratic and cubic regression model.
# We can use the anova() function to compare three models
anova(curvreg_lm,curvreg_Quadm) # Compare linear with quadratic regression model
## Analysis of Variance Table
##
## Model 1: perform ~ anxiety
## Model 2: perform ~ anxiety + anxiety_squared
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 12 2467.98
## 2 11 274.83 1 2193.2 87.779 1.412e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(curvreg_Quadm,curvreg_cubreg) # Compare quadratic with cubic regression model
## Analysis of Variance Table
##
## Model 1: perform ~ anxiety + anxiety_squared
## Model 2: perform ~ anxiety + anxiety_squared + anxiety_third
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 11 274.83
## 2 10 270.75 1 4.0833 0.1508 0.7059
anova(curvreg_lm,curvreg_cubreg) # Compare linear with cubic regression model
## Analysis of Variance Table
##
## Model 1: perform ~ anxiety
## Model 2: perform ~ anxiety + anxiety_squared + anxiety_third
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 12 2467.98
## 2 10 270.75 2 2197.2 40.577 1.589e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1