0
$\begingroup$

I'm doing an exercise on R about the hypothesis testing and I have the following data:

| Well depth (mts) | Water temperature (°C) |
|------------------|------------------------|
| x                | y                      |
| 600              | 200                    |
| 650              | 230                    |
| 700              | 280                    |
| 800              | 300                    |
| 800              | 310                    |
| 1000             | 350                    |
| 1000             | 330                    |
| 1000             | 360                    |
| 1500             | 410                    |

The hypothesis are:

$H_{0}:$ The depth does not affect the water temperature

$H_{1}:$ The depth DOES affect the water temperature

To achieve that, I have the following code:

well_depth <- c(600, 650, 700, 800, 800, 1000, 1000, 1000, 1500)
water_temp <- c(200, 230, 280, 300, 310, 350, 330, 360, 410)
geoterm_sys <- data.frame(well_depth, water_temp)

#lm (y ~ x, data)
lm_sist_geotem <- lm(geoterm_sys$water_temp ~ geoterm_sys$well_depth, data=geoterm_sys)
# Print linear model
lm_sist_geotem
# Get statistical info about the model 
summary(lm_sist_geotem)

Which gives me the following information:

Residuals:
   Min     1Q Median     3Q    Max 
-43.78 -24.65  12.75  19.28  29.28 

Coefficients:
                        Estimate Std. Error t value Pr(>|t|)    
(Intercept)            113.37638   33.65544   3.369 0.011941 *  
geoterm_sys$well_depth   0.21734    0.03615   6.013 0.000535 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 28.05 on 7 degrees of freedom
Multiple R-squared:  0.8378,    Adjusted R-squared:  0.8146 
F-statistic: 36.16 on 1 and 7 DF,  p-value: 0.0005353

So far, so good. I get a t value=6.013 that matches my manual calculations. After that I get the t values from table with this:

qt(.025, df=nrow(geoterm_sys)-1) #t=-2.306004

Having a two-tailed test, I reject the null hypothesis.

However, when I run this:

t.test(x=well_depth, y=water_temp, data=geoterm_sys)

I get the following values:

Welch Two Sample t-test

data:  well_depth and water_temp
t = 6.2412, df = 8.8993, p-value = 0.0001586
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 373.6589 799.6745
sample estimates:
mean of x mean of y 
 894.4444  307.7778 

Which do not match at all the previous calculations that I made. Specifically the T or even the df.

Even if I run the command only for x, the results do not match:

t.test(x=well_depth, y=NULL, data=geoterm_sys)

Which gives me this:

One Sample t-test

data:  well_depth
t = 9.7801, df = 8, p-value = 1.002e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
  683.5466 1105.3423
sample estimates:
mean of x 
 894.4444 

Can anybody explain why? Am I doing something wrong on the t.test command? Should I worry about the numbers?

Thanks!

Edit: Added hypothesis.

$\endgroup$
  • $\begingroup$ Hi. Basically the deeper the well, the hotter the water is. $\endgroup$ – StrayChild01 Nov 15 '18 at 17:29
0
$\begingroup$

Your tests are testing completely different (actually almost contrary) hypotheses:

  • The linear model tests if x and y correlate. You can easily replicate that by running something like

    cor.test(~well_depth + water_temp) # t = 6.0129, df = 7, p-value = 0.0005353

  • The two-sample t-test checks the null hypothesis that x and y come from distributions with equal expectation.

Now, assume e.g. that the x values are very similar to y. The t-test will fail to reject the null that their means are identical, while the linear regression will reveal a super strong relationship:

set.seed(345)

x <- 1:30
y <- x + rnorm(30)

plot(y ~ x)
cor.test(~ y + x) # t = 42.54, df = 28, p-value < 2.2e-16
t.test(x, y) # t = 0.029289, df = 57.989, p-value = 0.9767

enter image description here

$\endgroup$
  • $\begingroup$ So what you're saying is that t.test test (sorry for the redundance) checks whether the distribution is t shaped whereas cor.test checks if there is correlaction between the two variables?🤔 $\endgroup$ – StrayChild01 Nov 15 '18 at 17:50
  • $\begingroup$ Second one is correct, first one not. The two sample t test checks the null that the two population means are identical. $\endgroup$ – Michael M Nov 15 '18 at 17:58
  • $\begingroup$ I see. Somehow in my head --don't ask me why-- I understood it as: Calculate the t from the data and then comparate the calculated t against those from the tables at 95% confidence (if not provided) with n-1 degrees of freedom. $\endgroup$ – StrayChild01 Nov 15 '18 at 18:20
  • $\begingroup$ You can actually do this, but you would need to calculate the t statistic behind the standard correlation test! $\endgroup$ – Michael M Nov 15 '18 at 18:31
  • $\begingroup$ Something like this, right? set.seed(345) ;x <- 1:30 ;y <- x + rnorm(30) ; my_cor <- cor.test(~ y + x) ;print(my_cor$statistic);print(paste("calculated t=",qt(.025, df=length(x)-1)));#confidence interval -2.04523 < t < 2.04523 not true, then reject $\endgroup$ – StrayChild01 Nov 15 '18 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.