Question: What are their values now? Are the differences between coefficients for Virginia and West Virginia much smaller than they were in Part 4 Question 2?

 What are their values now? Are the differences between coefficients for

What are their values now? Are the differences between coefficients for Virginia and West Virginia much smaller than they were in Part 4 Question 2? 4. (10 points) You will write a function to encapsulate the tasks you performed in the previous questions. The function will be called reg . coef( ), and it will take in five arguments: - census . subset, a data frame, whose rows are a subset of the full census data frame; - x1, x2, two strings that match column names in census . subset, representing predictor variables for the regression; - y, another string that matches a column name in census . subset, representing a response variable for the regression; - shuffle, a Boolean variable, whose default value is FALSE. Your function should perform the following. If shuffle is TRUE, then the entries in census . subset$State_name are randomly permuted. If shuffle is FALSE, then this step is not performed (and census . subset$5tate_name is left alone). Next, for each state in the census . subset data frame, run a regression of the response, represented by y, on predictor variables, represented by x1 and x2. (Hint: you may create the formula in lm( ) using paste( ).) A matrix with 2 columns is returned, and one row for each state in census . subset. Each row gives the coefficients for x1 and x2 in the linear regression model computed for that particular state (though they are included in the regressions, the intercept terms here are ignored). (Hint: for running regressions at a state-bystate level, use dap1y( ) .) Recreate the results of Part 4 Questions 2&3 with your function, to check that it gives the same answers. Be sure to use set . seed() function to enforce reproducibility. 5. Bonus (10 points) Lastly, we will finally implement our permutation test. At a high-level, the idea is to judge differences in coefficients, say, from Part 4 Question 2 between Virginia and West Virginia, to those in Part 4 Question 3 computed using scrambled data. To make our comparisons to more meaningful, we will repeat the scrambling of state labels (the permutations) multiple times. Write a function permutation . test() that takes in the following seven arguments: . census, the census data frame; - statel and state2, two strings that represent state names; - x1, x2, y, as in reg.coef( ); - num, perm, an integer with a default value of 100. This function will carry out the following tasks, in order

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!