Do all of this in R and Python!
Question 2 (20 points)
Write a function that calculates the sample covariance between two variables and save it in a separate script file. Here are the values of the two variables: x=[1.1,5.5,7.8,4.2,-2.7,-5.3,8.9] and y=[0.1,1.5,0.8,-4.2,2.7,-9.4,-2.8].
Hint: Define two vectors xv and yv using the values above. Then write a function that has the 2 vectors as inputs. The formula for the sample covariance is:
Evaluate the covariance with the two vectors given above in your main program where you need to import the function! This is important, do not define the function in the main script, but write a second file. I need to see whether you know how to import functions that are saved in separate files.
Do this in R and Python!
Question 3: Data Question (20 points)
Go to the following website: [url removed, login to view]
And download the data for: Workforce & Economic Development, 2010. There is a link to an Excel file. Import this dataset into R.
If you want to read up on what the various variables mean you can download the code book using the link on top of the page under Longitudinal Data Code Book.
Into one figure, make a scatterplot of unempl10 and smallbus10 (add title and axes labels). Below the first graph, make a histogram of unempl10(add title and axes labels). You need to combine both graphs into one figure with a top panel (the scatterplot) and a bottom panel (the histogram).
Run a regression that explains the Percent of the Population between age 16-64 that is Unemployed: unempl10
Use numbus10, succbus10, totemp10, smallbus10, comrehab10, and nilf10 as explanatory variables.
Predict the unemployment rate using the coefficient estimates from your regression and the average of each explanatory variable as predictors.
This may sound more complicated than it is. All you do is you calculate the mean of each explanatory variable and multiply that by the respective coefficient estimate from the regression. Then you add it all up, don't forget to add the intercept in your prediction.
Do this in R only!
Question 4: Function plotting (20 points)
Write a function for:
f(x)=?????log(|x|)-xx2/(3-x)if x<0if 0=x<2if 2=x
Then use this function and plot it for -5=x=5.
Do this in Python only!
Question 5: (20 points)
Define a matrix
A=??[url removed, login to view]?
Write a loop and using if commands replace all the numbers in the main diagonal (top left to bottom right diagonal) with -5. You need to write a loop or find another automatic way to replace the numbers in the main diagonal. I will not give you points if you simply type
In this exercise you need to demonstrate how to loop over an array and how to use if - statements!
Write a loop and replace numbers in the off-diagonal with 100. Again, use a loop and if commands in order to get full points, simply typing the new matrix
will not result in any points.