Not getting the desire record Pearson results

Viewed 17

Any help is greatly appreciated. I did get help with cleaning up the data so it works great that way, no errors. But it's not the same values so I'm not sure where it's wrong. I've tried even running the cor.test a few different ways and don't come up with the right values. It won't allow me to upload the dataset because it isn't local, but it is nycflights13 found here https://nycflights13.tidyverse.org/

Here's my initial code:

suppressPackageStartupMessages(library(nycflights13))
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(lm.beta))
suppressPackageStartupMessages(library(data.table))
Q1 <- (conf.level = 99.45)  #based on Q2 values .. confidence level
delay_thresh = quantile(flights$dep_delay, p=c(0.003, 0.997), na.rm=T)
dist_thresh = quantile(flights$distance,p=c(0.003, 0.997), na.rm=T)
                      
nycflights13_DT <- as.data.table(flights)
nycflights13_clean <- nycflights13_DT[nycflights13_DT$dep_delay > delay_thresh[[1]] & 
                                        nycflights13_DT$dep_delay < delay_thresh[[2]] & 
                                        nycflights13_DT$distance>dist_thresh[[1]] & 
                                        nycflights13_DT$distance < dist_thresh[[2]]]
Q2 <- cor.test(nycflights13_clean$dep_delay, nycflights13_clean$distance)               
model2 = (lm(nycflights13_clean$dep_delay ~ nycflights13_clean$distance))
Q3 <- summary(model2)

So then I run:

Q2 <- cor.test(nycflights13_clean$dep_delay, nycflights13_clean$distance)               
model2 = (lm(nycflights13_clean$dep_delay ~ nycflights13_clean$distance))

This is what I'm supposed to get:

t = -14.451, df = 326677, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.02870131 -0.02184737
sample estimates:
        cor 
-0.02527463

This is what I get:

Pearson's product-moment correlation

data: nycflights13_clean$dep_delay and nycflights13_clean$distance

t = -13.647, df = 316421, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.02773612 -0.02077163
sample estimates:
       cor 
-0.02425417

This is what I'm supposed to be doing: Run cor.test for the relationship between departure delay and distance. Q2, do not round. I can do almost anything in Python and NumPy but R has me scratching my head.

0 Answers
Related