Lecture 3 and Lab 1

Oct 2, 2026





HW3 (Due on Monday Oct 5)

Download the data file, which contains brain weight and body weight of 62 species, move it to a proper location in google drive or your local drive if you have jupyter notebook locally.

a. Import the data to R.

b. Draw a scatter plot, and you can see that the current scale is not the best for display. You can apply a log-transformation on both variables. This can be done by using the log() function, you can put the old data.frame in the parenthesis, and assign the output a name so that you will have a new data.frame of the transformed data, something like below

> new.data <- log(old.data)

Draw a scatter plot of the new data,  does it look much better.

c. Fit a linear model on the original data. Draw plot the residual against the predictor using something similar to

> plot(old.data$\$$BodyWeight, lm.fit$\$$res)

d. Fit a linear model on the log-transformed data. Draw a plot the residual against the predictor. What do you see now?

e. Use the QQ-plot of the residuals against the theoretical normal distribution, (use function qqnorm) . What do you observe?


Regression towards the mean


Regression towards the mean is a common phenomenon in our daily life. Stock traders/fund managers that had the best return last year are likely to see their numbers down this year. In sports, many rookie sensations will go back to the earth in their second year. To explain such phenomenon, we must understand that life is full of randomness, top performers need some luck beside their ability, but luck does not stay with them forever, and their performance will drop when their luck is gone. The same thing happens to the bottom performers as well, bad lucks does not stay forever either.

Understanding such phenomenon has some practical implications as well. For example, if drugs are only given to those with the highest cholesterol levels,  you will see their numbers drop a couple months later even when the drug is not effective at all. Same can be said for the tutoring sessions given to the worst performing students. To truly evaluate the effectiveness of a treatment, we must set up a comparable control group, preferably by random assignment. And this can apply to the regression analysis itself, an SNP marker that has the lowest P-value in a GWAS study (in which SNPs markers are examined for their association with phenotypes) almost always show weaker association in the replication.

Correlation coefficients

Correlation coefficients (or simply correlation) measures the strength of linear association between two variables, we usually denoted as $r$. It can be computed as

$$r = \frac{\sum (x_i-\bar{x})(y_i-\bar{y})}{\sqrt{\sum(x_i-\bar{x})^2}\sqrt{\sum(y_i-\bar{y})^2}}$$

As you may guess, its square equals to $R^2$ from the least square fit of a regression model. Below are a couple of notes.

1. Correlation coefficients do not distinguish between two variables which one is the response and which one is the predictor. That is, the correlation between variables x and y is the same as the correlation between y and x.
2. The regression model, on the other hand, does distinguish response from predictors. However, if you switch the role between two variables, the $R^2$ remain the same.
3. The value of a correlation coefficient is between -1 and 1. The sign reflects if two variables are positively or negatively correlated (thinking straight lines with a positive or negative slope).
4. If all points line up on a straight line, the correlation coefficient is 1 or -1, depending on the slope of the line.
5. A correlation close to zero implies a weak linear relationship between two variable, but not necessarily a weak relationship.
6. If x or y has no variation, that is, x (or y) always takes the same value, the correlation coefficient is not well defined.  

To obtain a correlation coefficient in R, you can use the function cor(). cor(father.son) gives the correlation between two variables in the data.frame. If a data.frame contains more than two variables. cor() will give you all pair-wise correlation coefficients between those variables. Alternatively, cor(x,y) gives the correlation of variables $x$ and $y$.

Measures of Relationship between Two Variables


Pearson Correlation Coefficient is good for measure linear relationship but it is not designed to measure other kind of  relationships, non-linear, non-monotonic, non-functional, etc. Other statistical measures have been developed for capturing relationship between two variables. For example, Spearman correlation is a very popular statistics good for measure non-linear monotonic relationshipk, Hoeffiing's D measure is not very well known but is good for non-monotonic and non-functional relationship. Those who are interested can find some details in an excellent paper by Susan de Siqueira Santos et al, "A comparative study of statistical methods used to identify dependencies between gene expression signals". Briefings in Bioinformatics. vol. 15,6 (2014): 906-18.

Statistical Inference of Linear Models


One question that we did not ask but took the yes answer for granted is, "is son's height related to father's height?". Well, this is the type of question we often want to ask in our scientific investigation or even in political debate. Is human activity related to global climate change? Is drinking large soda related to obesity? Is the level of minimum wage related to job creation?

In a linear regression model $y = \beta_0 + \beta_1 x + \epsilon$, if $\beta_1=0$, the model reduces to $y = \beta_0 + \epsilon$, implies that $y$ is not related to $x$. For this implication to be valid, we have to assume that the error term $\epsilon$ is not related to $x$. That is, the distribution of error term $\epsilon$ does not vary on $x$. This is an assumption that is often made for statistical inference but it is not something that you should take for granted.

The Figure below shows two examples. In the left panel, it seems that the mean of response $y$ does not change along $x$, but its variation does. In the right panel, the best straight line model will likely to flat but $y$ is clearly related to $x$. In both cases, $\beta_1=0$ but the error term $\epsilon$ depend on $x$, either its variance (left) or its mean (right).

Well, that said, if you take a look at the data and find no strange patterns that set off the alarm, which is more often than not, you can be relaxed and let the assumption stand and move on to perform a hypothesis testing on $H_0: \beta_1=0$ vs $H_1:\beta_1 \neq 0$.

The test statistics used here is F-statistics, which is closely related to $R^2$,
$$F = \frac{(TSS-RSS)/(p-1)}{RSS/(n-p)}.$$ This statistics is a ratio between the part of the variation explained by the model to the variance of the error term. The larger it is, the model evidence supporting the alternative hypothesis. As you may have guessed, F-statistics is named after R. A. Fisher. To find the P-value, you look at where the statistic lands in the F-distribution with degrees of freedom (p-1, n-p). Yes, the F-distribution has two entries of degrees of freedoms, one for the numerator, the other for the denominator.

Now you can find the value of F-statistics at the bottom of what <span style="font-family:courier">summary(lm.fit)</span> returns, and the associated P-value. But let's also get this directly using the formulae.

Residual Plots and Diagnostics


Plot the residuals against the predictor is a good way to check the assumption that the error term does not depend on the predictor, in a more fancy term, being homoscedastic (pron.: /ˌhoʊmoʊskəˈdæstɪk/). A more general way of residual plots, especially when you have multiple predictors, is to plot the residuals against the fitted value, that is, plot $\hat{y}_i$ on the x-axis and $r_i=y_i-\hat{y}_i$ on the y-axis. In HW3, you should produce a residual plot for the linear regression model of brain weight with body weight as the predictor. On the original scale of the weight data, the residual plot clearly showed a pattern of heteroscedasticity (the opposite of homoscedasticity), that is, residuals tend to have a greater variation as the predictor increases. Although the least square fits still make sense, the violation of the assumption will throw off the statistical inferences such as the F-test. Hence, the process of checking the basic assumptions is critical in every regression analysis. Such process is termed "regression diagnostics".

In addition, you can use QQ plot to check if residuals follow a normal distribution. This assumption, although useful, is not critical for statistical inference with large sample size. The plot, however, let you see if you have some large residuals, which might set the alarm off for outliers.

Magic of Transformation


To deal with heteroscedasticity, a simple but powerful approach is to simply transform the response, and some time to transform the predictors too in order to maintain the linear relationship. Logarithm is by far the most popular transformation. Transformation is a very effective traditional approach, that is alternative to the more modern technique of generalized linear models, which in my opinion, are over-used in applications such as RNA-seq analysis.

In the homework, once you apply the log-transformation on both brain weight and body weight, the residual plot shows no pattern of residuals increasing in size as weight increases. Moreover, you can show with QQ plots that the (standardized) residuals fits the normal distribution very well (almost perfectly).


Leverage, Outliers and Influential Points

The idea of leverage is illustrated in the figure below. There are 100 observations, all but one stays on the lower left corner of the scatter plot. You can imagine that if the lonely point on the right moves up and down, the least square straight line will move along with it. This point is said to have high leverage in determining the model. Mathematically, an observation has high leverage if it is farther away from others in terms of their values at predictors, and the standardized residuals are adjusted for that. For you, it is probably more important to be aware of such phenomenon than knowing the mathematical definition of leverage and the formula to adjust for it.





The Figure below illustrates how a single observation can change the sign of the relation between two variables. The point on the up-right corner has high leverage and pulls the least square line towards it, showing a positive correlation. However, if this observation is not included, we suddenly see a negative correlation between these variables. In this case, the point on the up-right corner not only has high leverage but is also an influential point, as the linear model are dramatically different with or without it being included in the study. I have emphasized the importance of visualization of the data at the very beginning, and this is one of those examples that you can see much more easily on a plot than from the numbers. The influential point might be real, or might be recording or experimental error. In the case of former, you need to describe the complete story of the data rather than reporting a positive relationship between two variables.




Finally, one should not mix up influential points with outliers. The former is more likely to be those of high leverage, and the latter are those of extremely large residuals. An outlier could have similar values of other observation in predictors, but its value of the response is out of the pattern, resulting in large residuals. Identifying outliers is the underlying principle of complicated fraud detection algorithms used by corporations and government agencies.

Comments

Popular posts from this blog

Final Exam

Syllabus

Lecture 2