Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

R studio question Can someone please help me to check if my code has problems.Especially I got exactly the same results on the last two

R studio question

Can someone please help me to check if my code has problems.Especially I got exactly the same results on the last two questions every time but I don't think the random for loop will generate the same results .

Question:

  1. What is the difference between the average education level of men and women in your sample?
  2. What is the difference between the s-squared of education for men in your sample and the s-squared for education for women in your sample?
  3. Test the null hypothesis that men and women have the same average education. Perform a two-sided test, without assuming equal variances. Report the resulting p-value.
  4. Set a seed equal to 2108. Create a new variable in your data set called "Sex_Randomized" and randomly assign each observation to be male or female.

a. Find the difference between the mean of education for those randomly assigned "male" and those randomly assigned "female"

b. Find the difference between the s-squared for the sample of observations randomly assigned male and the sample randomly assigned female

2. Set your seed to the number 2108 again. Immediately after setting the seed, use loop that does the following things 1,000 times

  • Create random assignment of sex in the same way you did in question 4.
  • Compute the difference in mean of education between those randomly assigned "Male" and "Female" the same way you did in 4a and store that difference in a vector.
  • Compute the difference in s-squared of education between those randomly assigned "Male" and "Female" the same way you did in 4b and store that difference in a vector.

After completing that loop report the following things:

a. What fraction of the random assignments produced a difference in means that was larger in absolute value than the actual difference in mean education between actual men and women in the sample (your answer to 1.)?

b. What fraction of the random assignments produced a difference in s-squareds that was larger in absolute value than the actual difference in s-squareds of education between actual men and women in the sample (your answer to 2.)?

My code:

image text in transcribedimage text in transcribed
library(dplyr) data %summarise("woman_random"=mean(Education [Sex_Randomized=="Female"]), "man_random"=mean(Education [Sex_Randomized=="Male"]), "Diff"=woman_random-man_random) #4b data%>%summarise("woman_random_var"=var(Education [Sex_Randomized=="Female"]), "man_random_var"=var(Education [Sex_Randomized=="Male"]), |"Diff"=woman_random_var-man_random_var)set . seed(2108) data$Sex_Randomized = sample(data$Sex, size = length(data$Sex)) N=1000 r1=vector(length=N, mode="numeric") r2=vector(length=N, mode="numeric") for (i in 1: 1000){ data$Sex_Randomized2 = sample(data$Sex, size = length(data$Sex)) result_mean=data%>%summarise("woman_random2"=mean(Education [Sex_Randomized2=="Female"]), "man_random2"=mean(Education [Sex_Randomized2=="Male"]), "Diff_mean"=woman_random2-man_random2) result_var=data%>%summarise("woman_random_var2"=var(Education[Sex_Randomized2=="Female"]), "man_random_var2"=var(Education [Sex_Randomized2=="Male"]), "Diff_var"=woman_random_var2-man_random_var2) r1[i]=result_mean$Diff_mean r2[i]=result_var$Diff_var} summaryCabs (r1)>0. 07518932) summaryCabs (r2)>0. 1410726)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elements Of Chemical Reaction Engineering

Authors: H. Fogler

6th Edition

013548622X, 978-0135486221

Students also viewed these Mathematics questions