Docsity

Cheat Sheet: Linear Regression, Study notes of Statistics

Toggle sidebar
bg1
Cheat&Sheet:&Linear&Regression&
Measurement&and&Evaluation&of&HCC&Systems&
&
Scenario)
Use&regression&if&you&want&to&test&the&simultaneous&linear&effect&of&several&variables&varX1,&
varX2,&…&on&a&continuous&outcome&variable&varY.&In&this&scenario,&you&are&predicting&varY&with&
varX1,&varX2,&….
Power)analysis)for)linear)regression)
- There&are&several&versions&of&power&analysis&for&linear&regression,&but&the&most&versatile&one&
is&“F&tests”,&“Linear&multiple&regression:&Fixed&model,&R2&increase”.&
- A&power&analysis&has&four&variables:&Effect&size,&
α
$(usually&.05),&power&(usually&.85),&and&N.&If&
you&know&three&of&these,&G*Power&will&calculate&the&fourth.&Select&the&correct&type&of&power&
analysis,&based&on&the&information&you&have,&and&what&you&want&to&find&out.&
- “Number&of&tested&predictors”&is&the&number&of&Xs&that&you&want&to&test&simultaneously.&
Often&you&want&to&test&the&effect&of&an&individual&X,&in&which&case&this&field&should&say&1.&
- “Total&number&of&predictors”&is&the&total&number&of&Xs&in&the&model.&
- By&clicking&on&“Determine”,&you&can&compute&the&effect&size&f2&from&the&partial&R2.&This&is&the&
increase&in&R2&caused&by&the&predictors&you&want&to&test.&
- Click&on&“Calculate”&to&calculate&the&missing&parameter.&
Plotting)scatterplots)with)linear)trend)line)
- Use&the&ggplot2&package&to&plot&a&scatterplot&with&a&linear&trend&line.&
ggplot(data, aes(varX1, varY)) + geom_point() + geom_smooth(method=”lm”,
color=”red”, se=F)&&
- (optional)&To&add&a&mean&line,&add:&
+ geom_line(aes(y = mean(data$varY)), color=“blue”&&
- Visually&inspect&if&the&relationship&is&indeed&a&linear&one&(see&assumptions).&You&can&also&
check&if&the&scatterplot&has&a&funnel&shape,&which&may&suggest&heteroscedasticity&(see&
assumptions).&Repeat&this&procedure&for&varX2,&etc.&
pf3
pf4
pf5

Partial preview of the text

Download Cheat Sheet: Linear Regression and more Study notes Statistics in PDF only on Docsity!

Cheat Sheet: Linear Regression

Measurement and Evaluation of HCC Systems

Scenario

Use regression if you want to test the simultaneous linear effect of several variables varX1, varX2, … on a continuous outcome variable varY. In this scenario, you are predicting varY with varX1, varX2, ….

Power analysis for linear regression

  • There are several versions of power analysis for linear regression, but the most versatile one is “ F tests”, “Linear multiple regression: Fixed model, R^2 increase”.
  • A power analysis has four variables: Effect size, α (usually .05), power (usually .85), and N. If you know three of these, G*Power will calculate the fourth. Select the correct type of power analysis, based on the information you have, and what you want to find out.
  • “Number of tested predictors” is the number of X s that you want to test simultaneously. Often you want to test the effect of an individual X , in which case this field should say 1.
  • “Total number of predictors” is the total number of X s in the model.
  • By clicking on “Determine”, you can compute the effect size f^2 from the partial R^2. This is the increase in R 2 caused by the predictors you want to test.
  • Click on “Calculate” to calculate the missing parameter.

Plotting scatterplots with linear trend line

  • Use the ggplot2 package to plot a scatterplot with a linear trend line. ggplot(data, aes(varX1, varY)) + geom_point() + geom_smooth(method=”lm”, color=”red”, se=F)
  • (optional) To add a mean line, add:
    • geom_line(aes(y = mean(data$varY)), color=“blue”
  • Visually inspect if the relationship is indeed a linear one (see assumptions). You can also check if the scatterplot has a funnel shape, which may suggest heteroscedasticity (see assumptions). Repeat this procedure for varX2, etc.

Pre-testing assumptions

  • In a linear regression, Y should be independent, continuous, and unbounded. The error variance should be normally distributed, which is true if Y is normally distributed.
  • If your N is small: o Test for significant skewness, kurtosis, and Shapiro-Wilk test using stat.desc in the pastecs package. stat.desc(data$varY, basic=F, norm=T) o Multiply skew.2SE and kurt.2SE by 2 to get the Z - scores of skewness and kurtosis. Compare these values to typical cut-off values ( Z > ±1.96: p < .05, Z > ±2.58: p < .01, Z > ±3.29: p < .001). The significance of the Shapiro-Wilk test is listed under normtest.p.
  • If your N is large: o Draw the histogram for varY, overlaid with normal curves (using ggplot2), and visually inspect whether they follow the normal distribution: ggplot(data, aes(varY)) + geom_histogram(aes(y=..density..), binwidth=1, color="black", fill="white") + stat_function(fun = dnorm, args = list(mean = mean(data$varY), sd = sd(data$varY))) o Change the binwidth setting based on what is suitable for your data. o Draw normal a Q-Q plot, and visually inspect whether the data follows the diagonal line: qplot(sample = data$varY, stat=”qq”)
  • If you have dummy variables (see below), you want to test normality within each group (see the t test and ANOVA cheat sheets).
  • If your data has positive skew, and your data only has positive values, you can possibly fix this by transforming your Y variable: o Log transform: data$varYlog <- log(data$varY + 1) o Or, square root transform: data$varYsqrt <- sqrt(data$varY)
  • In other cases of violations of assumptions, you can conduct a robust test (see below).
  • Aside from normality, you also need to test for linearity, homoscedasticity and a lack of multicollinearity. You can do this after the test using the residual plots of the model.

(optional) Preparing dummy variables

  • If one or more of your X s are nominal variables, you need to create dummy variables for them. R does this automatically, as long as the nominal X s are coded as factors. If they are coded as numerical values (e.g. 1, 2, 3…), you can recode them as factors using the revalue

expected to increase/decrease by this proportion of its standard deviation. Note: this does not work for dummy variables.

(optional) Robust versions

  • You can bootstrap the regression coefficients (the b s) of a linear regression. First create a function for running the bootstrap sample (include additional X s if needed): bootFun <- function(sample,i){ fit <- lm(varY~varX1+varX2, data=sample[i,]) return(coef(fit)) }
  • Then run the bootstrap sample over the function 2000 times: bootResult <- boot(data, bootFun, 2000)
  • Get the output; the original column shows the regression coefficient in the original sample, the bias column shows the difference between this and the coefficient in the bootstrap sample, and the std. error column shows the bootstrapped standard error. Row t1* shows the intercept, the subsequent rows show the coefficient b for each X : bootResult
  • Get the confidence interval of the first coefficient; the BCa version is the most robust variant: boot.ci(bootResult,index=2)
  • Repeat with index=3 etc. for the other coefficients.

(optional) Testing additional variables

  • In any regression, always first decide on your outcome variable ( Y ) and the most important X s. For additional X s, only include them if they correlate with Y. Do not include X s that are correlated too highly with any of the other X s ( r > .8 or r < – .8), because this may lead to multicollinearity (see below).
  • If these additional X s have a clear hierarchy (some are more interesting than others), then add them step-by-step in order of importance. Otherwise, add them all at once and remove non-significant ones one-by-one.
  • Models with more Xs always have a higher R^2. You can test whether this increase in R^2 is significant using an F - ratio test: anova(model1,model2)
  • Do not forget to assess the b estimates of the new model. Even the b estimates for the old parameters may have changed, especially when the new X s are correlated with the old X s.

Post-testing assumptions

  • Run the plots of the model: plot(model1)
  • The first plot shows the residuals ε at different levels of Y. If this plot is funnel-shaped, you have heteroscedasticity. If the red mean line is not a straight line, you have non-linearity.
  • The next plot shows the normal Q-Q plot of the residuals. If they deviate strongly from the diagonal line, you have non-normality.
  • With any of these problems, you can possibly fix this by transforming your Y variable (see “Pre-testing assumptions”), or by running a bootstrapped regression (see “Robust versions”).
  • Test for multicollinearity of your X s using the Variance Inflation Factor (VIF); these should be below 5: vif(model1)
  • Check the average VIF. Ideally this should be 1, but values close to (say, up to 1.5) are permissible: mean(vif(model1))
  • If you find any problems, some of your Xs are probably highly correlated with each other. Remove one of these Xs and start over.

Inspecting outliers

  • Save the standardized residuals to your data: data$rstand <- rstandard(model1)
  • Create a filter for large standardized residuals: data$rstand.large <- (data$rstand > 1.96 | data$rstand < - 1.96)
  • Inspect these outliers: data[data$rstand.large,]
  • If these outliers comprise more than 5% of the data, you have too many data for which the model does not fit very well. Moreover, if more than 1% of the data has an rstand > 2.58, you have too many data for which the model is a very poor fit. Finally, any data with an rstand > 3.29 are extreme outliers: they will likely have to be removed.
  • Save Cook’s distances, hat values (leverage), and covariance ratios to your data: data$cook <- cooks.distance(model1) data$leverage <- hatvalues(model1) data$covratio <- covratio(model1)
  • Check out the Cook’s distances, hat values, and covariance ratio for the data with a large residual: data[data$rstand.large,c(“cook”, “leverage”, “covratio”)]
  • Cook’s distance should be smaller than 1, leverage should be smaller than 3*( k +1)/ N ), and the covariance ratio should be between 1-(3( k +1)/ N ) and 1+(3( k +1)/ N ), where k is the number of X s, and N is the sample size.
  • These metrics are a bit sensitive to sample size, so for large samples, focus on the Cook’s distance. In case of problems, you can possibly fix this by removing the offending data point,