<jupyter_start><jupyter_text>My DH Project: How Moving Online Has Affected Students' Sleep in Non-pacific Time ZonesCourse: Digital HumanitiesInstructor: <NAME>Student: <NAME><jupyter_code>from datascience import *
import numpy as np
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
import pandas as pd
import os<jupyter_output><empty_output><jupyter_text>Generate Data Dataset was generated from the survey I made for Psych133: Psychology of Sleep * Project in Psych133 was about a Sleep Outreach Form. We created a survey in [English](https://docs.google.com/forms/d/e/1FAIpQLSdtmY7q00ED2fhGxnQZeMuP6grQZhSFjbgpkmLuYLWH-PkPdQ/viewform) and  [Chinese](https://wj.qq.com/s2/7616138/5b53/), spread it out and informed surveyors with sleep fun facts. Responses collected from UC Berkeley students<jupyter_code>data_url = "https://raw.githubusercontent.com/Sheldenshi/Sleep-Survey/main/survey_data.csv"
sleep = Table.read_table(data_url)
sleep<jupyter_output><empty_output><jupyter_text>Data Breakdown<jupyter_code>sleep.num_rows<jupyter_output><empty_output><jupyter_text>Number of people participated<jupyter_code>sleep.group("Pacific Time")<jupyter_output><empty_output><jupyter_text>Number of participants in Pacific Time zone and Non-Pacific Time zone  Data Interpretation<jupyter_code>sleep.group("Relationship")<jupyter_output><empty_output><jupyter_text>119 out of 131 people think that there is a positive relationship between sleep and academic performance<jupyter_code>#sleep needed
np.mean(sleep.column(1))<jupyter_output><empty_output><jupyter_text>On average people think humans need 7.66 hours/day<jupyter_code>#sleep time
np.mean(sleep.column(3))<jupyter_output><empty_output><jupyter_text>On Average students went to bed at 2.23 AM<jupyter_code>new = sleep.with_column("Get Up", sleep.column(3) + sleep.column(4))
#Get Up Difference
np.mean(new.where("Pacific Time", are.equal_to("Y")).column("Get Up")),np.mean(new.where("Pacific Time", are.equal_to("N")).column("Get Up"))<jupyter_output><empty_output><jupyter_text>Average wake up time Pacific Time:8:37 AM (student's local time) Non-Pacific Time:9:45 AM (student's local time)<jupyter_code>#sleep time difference
pacific = sleep.where("Pacific Time", are.equal_to("Y"))
no_pacific = sleep.where("Pacific Time", are.equal_to("N"))
np.mean(pacific.column(3)), np.mean(no_pacific.column(3))<jupyter_output><empty_output><jupyter_text>Average bedtimePacific Time:1:35 AM (student's local time)Non-Pacific Time:3:25 AM (student's local time)<jupyter_code>def gpamean(t, x):
    return np.average(t.column(x))

pacific_gpa = pacific.where("GPA", are.above (1))
shuffled = pacific_gpa.sample(with_replacement = False)
repetition = 1000
mean_gpa = make_array()

for i in np.arange(repetition):
    simulation = shuffled.sample(37, with_replacement = True)
    mean_gpa = np.append(mean_gpa, gpamean(simulation, 7))
       
lower = percentile(2.5, mean_gpa)
upper = percentile(97.5, mean_gpa)
round(lower,4), round(upper,4)
repetition = 1000
mean_gpa_ = make_array()
no_pacific_gpa = no_pacific.where("GPA", are.above (1))
shuffled_ = no_pacific_gpa.sample(with_replacement = False)

for i in np.arange(repetition):
    simulation_ = shuffled_.sample(with_replacement = True)
    mean_gpa_ = np.append(mean_gpa_, gpamean(simulation_, 7))  
    
lower = percentile(2.5, mean_gpa_)
upper = percentile(97.5, mean_gpa_)
round(lower,4), round(upper,4)
histbin = np.arange(3.4, 3.9, 0.01)
Table().with_columns("Pacific GPA", mean_gpa, "Non-Pacific GPA", mean_gpa_).hist(bins = histbin)<jupyter_output><empty_output><jupyter_text>Cal students who are in Pacific time tend to have higher average GPA than Non-Pacific students according to bootstrap test.<jupyter_code>#sleep hours
np.mean(sleep.column(4))<jupyter_output><empty_output><jupyter_text>Average hours of sleep (all students)<jupyter_code>#sleep hours difference
np.mean(pacific.column(4)), np.mean(no_pacific.column(4))<jupyter_output><empty_output><jupyter_text>Average hours of sleep:Pacific time: 7 hoursNon-Pacific time: 6.3 hours<jupyter_code>#quality
np.mean(sleep.column(5))<jupyter_output><empty_output><jupyter_text>Average sleep quality (all students on a scale of 1-5 self-rated)<jupyter_code>#quality difference
np.mean(pacific.column(5)), np.mean(no_pacific.column(5))<jupyter_output><empty_output><jupyter_text>Average sleep quality:Pacific time: 3.4 Non-Pacific time: 2.53<jupyter_code>#ability
np.mean(sleep.column(6))<jupyter_output><empty_output><jupyter_text>Average ability to sleep (all students on a scale of 1-5 self-rated)<jupyter_code>#ability difference
np.mean(pacific.column(6)), np.mean(no_pacific.column(6))<jupyter_output><empty_output><jupyter_text>Average ability to sleep:Pacific time: 3.2 Non-Pacific time: 2.77<jupyter_code>#GPA
gpa = sleep.where("GPA", are.above(1))
np.mean(gpa.column(7))<jupyter_output><empty_output><jupyter_text>Average GPA of all students<jupyter_code>#GPA difference
np.mean(gpa.where("Pacific Time", are.equal_to("Y")).column(7)), np.mean(gpa.where("Pacific Time", are.equal_to("N")).column(7))<jupyter_output><empty_output><jupyter_text>Average GPA:Pacific time: 3.66 Non-Pacific time: 3.59<jupyter_code>#sleep time for high GPA, sleep time for low GPA
gpa1 = gpa.where("GPA", are.above(3.60))
gpa2 = gpa.where("GPA", are.below(3.60))
np.mean(gpa1.column(3)), np.mean(gpa2.column(3))<jupyter_output><empty_output><jupyter_text>Student who has GPA above 3.6 went to bed at 1:47 AM on averageStudent who has GPA below 3.6 went to bed at 2:30 AM on average<jupyter_code>#sleep hours for high GPA, sleep hours for low GPA
np.mean(gpa1.column(4)), np.mean(gpa2.column(4))<jupyter_output><empty_output><jupyter_text>Student who has GPA above 3.6 on average slept 7 hours a night this past semesterStudent who has GPA below 3.6 on average slept 6.68 hours a night this past semester<jupyter_code>#quality for high GPA, quality for low GP
np.mean(gpa1.column(5)), np.mean(gpa2.column(5))<jupyter_output><empty_output><jupyter_text>Students with higher GPA reported higher sleep quality<jupyter_code>#ability for high GPA, ability for low GPA
np.mean(gpa1.column(6)),np.mean(gpa2.column(6))<jupyter_output><empty_output><jupyter_text>Students with higher GPA reported better ability to fall asleep<jupyter_code>sleep.relabel("Sleep Hours", "Sleep Received").hist("Sleep Needed", "Sleep Received")<jupyter_output><empty_output><jupyter_text>Amount of sleep students think they need VS amount of sleep received for the past semester.<jupyter_code>sleep.hist("Quality","Ability")
def standard_units(any_numbers):
    return (any_numbers - np.mean(any_numbers))/np.std(any_numbers) 
  
def correlation(t, label_x, label_y):
    return np.mean(standard_units(t.column(label_x))*standard_units(t.column(label_y)))

def slope(t, label_x, label_y):
    r = correlation(t, label_x, label_y)
    return r*np.std(t.column(label_y))/np.std(t.column(label_x))

def intercept(t, label_x, label_y):
    return np.mean(t.column(label_y)) - slope(t, label_x, label_y)

def fit(table, x, y):
    a = slope(table, x, y)
    b = intercept(table, x, y)
    return a * table.column(x) + b
                                              
def residual(table, x, y):
    return table.column(y) - fit(table, x, y)

def scatter_fit(table, x, y):
    table.scatter(x, y, s=15)
    plots.plot(table.column(x), fit(table, x, y), lw=4, color='gold')
    plots.xlabel(x)
    plots.ylabel(y)                             
    
def residual_plot(table, x, y):
    x_array = table.column(x)
    t = Table().with_columns(
            x, x_array,
            'residuals', residual(table, x, y)
        )
    t.scatter(x, 'residuals', color='r')
    xlims = make_array(min(x_array), max(x_array))
    plots.plot(xlims, make_array(0, 0), color='darkblue', lw=4)
    plots.title('Residual Plot')
    

scatter_fit(gpa, "Sleep Time", "GPA")
print(correlation(gpa, "Sleep Time", "GPA"))<jupyter_output>-0.13316869261206643<jupyter_text>There is a negative correlation between bedtime(hours from 10 pm) and GPA, with a 0.133 r-value.<jupyter_code>scatter_fit(gpa, "Sleep Hours", "GPA")
print(correlation(gpa, "Sleep Hours", "GPA"))<jupyter_output>0.024583184465485384
{'fraction_non_alphanumeric': 0.12892662219526987, 'fraction_numerical': 0.02607640994542147, 'mean_word_length': 6.290893015030946, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '49406', 'license': 'Unlicense', 'n_tokens_mistral': 3211, 'n_tokens_neox': 2964, 'n_words': 639, 'path': 'dighum100_notebook.ipynb', 'repo_name': 'fuwentan2021/DH100-project', 'size': '8255'}