###################################################################################################################### #Generate non linearity ###################################################################################################################### #Defining the Sample Size and the increasing variance scale size <- 20 X <- seq(0,size,by=0.5) # Generating observations with non linearity Y <- -2*X^2 + 40*X + rnorm(length(X),0,4) fit <- lm(Y~X) ## Plot the original data and the fitted line plot(x=X,y=Y) lines(x=X,y=predict(fit),col="red") ## Plot the residual x predicted value to validate the hypothesis plot(x=predict(fit),y=residuals(fit)) ## Perform the normal Shapiro-Wilk test for the residuals shapiro.test(residuals(fit)) ###################################################################################################################### #Fixing the problem including a quadratic term X ###################################################################################################################### X1 <- X X2 <- X^2 fit <- lm(Y~X1+X2) ## Plot the residual x predicted value to validate the hypothesis plot(x=predict(fit),y=residuals(fit)) ## Perform the normal Shapiro-Wilk test for the residuals shapiro.test(residuals(fit))