<jupyter_start><jupyter_text>Problem set 5- Below are the functions necessary to complete problem set 5- We will use an internal data set that is available in r-studio  A.1 Import packages<jupyter_code>library(dplyr) #Data manipulation<jupyter_output>Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union<jupyter_text>1.   1.1 Create table- PS5 corresponding problem 1.a<jupyter_code>n = 164+160

#Change x number to get answer for your assignment
x = n-20-10 #-NUMBER #-NUMBER

p = x/n
table_1 = cbind(x,n,p)
table_1<jupyter_output><empty_output><jupyter_text>1.2 Standard error calculation- PS5 corresponding problem 1.b$$SE_{\hat{p}}= \sqrt{\frac{p(1-p)}{n}}$$<jupyter_code>answer_function_12 = function(sampling_proportion, n){
    step_1 = sampling_proportion*(1-sampling_proportion)
    step_2 = step_1/n
    step_3 = sqrt(step_2)
    
    return (step_3)
}
standard_error = answer_function_12(p, n)
standard_error<jupyter_output><empty_output><jupyter_text>1.3 Create 95% CI interval- PS5 corresponding problem 1.c$$\hat{p}\pm z_{\alpha/2}SE_{\hat{p}}$$<jupyter_code>answer_function_13 = function(sampling_proportion, z, std_error){
    
    rhs = z*std_error
    
    conf_lower = p-rhs
    conf_upper = p+rhs
    
    print(paste('p +-', round(z,3),'*',round(std_error,3), sep=''))
    print(paste('Upper bound: ', conf_upper, ' Lower bound: ', conf_lower, sep=''))
}
#Change z number to get answer for your assignment
z = 1.5


answer_function_13(p, z, standard_error)<jupyter_output>[1] "p +-1.5*0.016"
[1] "Upper bound: 0.931562462719905 Lower bound: 0.88325235209491"<jupyter_text>1.4 Create 95% CI interval- PS5 corresponding problem 1.d<jupyter_code>prop.test(x,n, p=.25, correct=F)<jupyter_output><empty_output><jupyter_text>2.   2.1 Table- PS5 corresponding problem 2.a<jupyter_code>table_21 = matrix(nrow=2, ncol=3, c(68,164,128,204,173,165))
table_21<jupyter_output><empty_output><jupyter_text>2.2 Chi-square expected values- PS5 corresponding problem 2.c- chisq.test function [info](https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/chisq.test)    - chisq.test()$expected        - the expected counts under the null hypothesis.  2.2.1 chisq function<jupyter_code>chisq.test(table_21 , correct=F)$expected<jupyter_output><empty_output><jupyter_text>2.2.2 by hand (OPTIONAL)$$\frac{\text{Row_total}*\text{column_total}}{\text{total_obs}}$$<jupyter_code>table_21_full = as_tibble(table_21)  %>% mutate(total= rowSums(.))
table_21_full = rbind(table_21_full, colSums(table_21_full))
table_21_full<jupyter_output>Warning message:
“`as_tibble.matrix()` requires a matrix with column names or a `.name_repair` argument. Using compatibility `.name_repair`.
This warning is displayed once per session.”<jupyter_text>2.2.2.1 Calculate with sums (3rd row & 4th column)<jupyter_code>#3rd row
col_sums = table_21_full[3,1:3]
col_sums
#4th column
row_sums_1 = as.numeric(table_21_full[1,4])
row_sums_2 = as.numeric(table_21_full[2,4])
print(row_sums_1)
print(row_sums_2)
col_sums*row_sums_1/902
col_sums*row_sums_2/902<jupyter_output><empty_output><jupyter_text>2.3 Chi-square test for null that age provides information about the approval for stop and frisk<jupyter_code>chisq.test(table_21 , correct=F)<jupyter_output><empty_output><jupyter_text>3. Chi square 3.1 - PS5 corresponding problem 3.b    - Similar to 2.2.1<jupyter_code>#fridays = c()
#chisq.test(fridays, correct=F)$expected<jupyter_output><empty_output><jupyter_text>3.2PS5 corresponding problem 3.c   - Similar to 2.3<jupyter_code>#probabilities = #c(#,#,#)
#chisq.test(fridays, correct=F, p=probabilities)<jupyter_output><empty_output><jupyter_text>4. Regression (OPTIONAL)- [Data](https://ideas.repec.org/p/boc/bocins/bwght.html)  4.1 Download data<jupyter_code>library(foreign)
url = 'http://fmwww.bc.edu/ec-p/data/wooldridge/bwght.dta'
#url = 'http://fmwww.bc.edu/ec-p/data/wooldridge/bwght2.dta'
df_birth = read.dta(url)
df_birth %>% head(2)
plot(df_birth$cigs, df_birth$bwght)
abline(lm(bwght~cigs, data=df_birth), col="red")<jupyter_output><empty_output><jupyter_text>4.2 Regression- lm function<jupyter_code>lm(bwght~ cigs, data= df_birth)<jupyter_output><empty_output><jupyter_text>4.2.1 Coefficients<jupyter_code>coefs = lm(bwght~ cigs, data= df_birth)$coefficients
coefs<jupyter_output><empty_output><jupyter_text>4.3 Calculate expected weight given smoked cigs a dayPS5 corresponding problem 4.d<jupyter_code>beta0 = coefs[c(1)] %>% as.double()
beta0 
beta1 =coefs[c(2)] %>% as.double()
beta1 
cigs_day = 10
beta0 + cigs_day*beta1<jupyter_output><empty_output><jupyter_text>4.3.1 Input data<jupyter_code>var = readline(prompt = "Enter number of cigs smoked a day : "); 
  
# convert the inputted value to an integer 
var1 = beta0 + beta1*as.integer(var)
  
# print the value 
print(paste('Smoking ', var, ' cigs/day, we expect a child to be ', round(var1,3), ' ounces', sep=''))<jupyter_output>Enter number of cigs smoked a day : 10
[1] "Smoking 10 cigs/day, we expect a child to be 114.634 ounces"
{'fraction_non_alphanumeric': 0.1351614157915208, 'fraction_numerical': 0.048035783741734735, 'mean_word_length': 6.203081232492997, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '288401', 'license': 'MIT', 'n_tokens_mistral': 2141, 'n_tokens_neox': 1991, 'n_words': 392, 'path': 'R_basics/Disc9_PS5.ipynb', 'repo_name': 'corybaird/PLCY_Code_2020_winter', 'size': '5142'}