You are on page 1of 18

Scanned by CamScanner

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
1. Write a functions in R that take a vector as input and returns:

a.This function find the last value in a vector.

1. last_value <- function(x) {


2. if (length(x)) {
3. x[[length(x)]]
4. } else {
5. x
6. }
7. }

b. This function returns the elements at even number positions.

8. even_indices <- function(x) {


9. if (length(x)) {
10. x[seq_along(x) %% 2 == 0]
11. } else {
12. x
13. }
14. }

c.This function returns a vector with every element except the last.

15. not_last <- function(x) {


16. n <- length(x)
17. if (n) {
18. x[-n]
19. } else {
20. # n == 0
21. x
22. }
23. }

d. This function returns the elements of a vector that are even numbers.

24. even_numbers <- function(x) {


25. x[x %% 2 == 0]
26. }

even_numbers2 <- function(x) {


x[!is.infinite(x) & !is.nan(x) & (x %% 2 == 0)]
}
*************************************************************************************

Question2.

[1] 1 2

[1] 1 2 3
13. Why is it desirable to remove insignificant variables from a regression?
13Answer . By definition, variables having associated parameters that are not significantly
different from zero are not, from a statistical perspective, helping to explain variations in the
dependent variable about its mean value. One could therefore argue that empirically, they serve no
purpose in the fitted regression model. But leaving such variables in the model will use up valuable
degrees of freedom, implying that the standard errors on all of the other parameters in the
regression model, will be unnecessarily higher as a result. If the number of degrees of freedom is
relatively small, then saving a couple by deleting two variables with insignificant parameters could
be useful. On the other hand, if the number of degrees of freedom is already very large, the impact
of these additional irrelevant variables on the others is likely to be inconsequential.

14. What might Ramsey’s RESET test be used for? What could be done if it were found that the
RESET test has been failed?[4 marks]

Ramsey’s RESET test is a test of whether the functional form of the regression is appropriate. In
other words, we test whether the relationship between the dependent variable and the independent
variables really should be linear or whether a non-linear form would be more appropriate. The test
works by adding powers of the fitted values from the regression into a second regression. If the
appropriate model was a linear one, then the powers of the fitted values would not be significant
in this second regression.

If we fail Ramsey’s RESET test, then the easiest “solution” is probably to transform all of the
variables into logarithms. This has the effect of turning a multiplicative model into an additive
one.

If this still fails, then we really have to admit that the relationship between the dependent variable
and the independent variables was probably not linear after all so that we have to either estimate a
non-linear model for the data (which is beyond the scope of this course) or we have to go back to
the drawing board and run a different regression containing different variables.

15. What is an estimator? Is the OLS estimator superior to all other estimators? Why or why not?
Answer. An estimator is simply a formula that is used to calculate the estimates, i.e. the
parameters that describe the relationship between two or more explanatory variables. There are
an infinite number of possible estimators; OLS is one choice that many people would consider a
good one. We can say that the OLS estimator is “best” – i.e. that it has the lowest variance
among the class of linear unbiased estimators. So it is omptimal in the sense that no other linear,
unbiased estimator would have a smaller sampling variance. We could define an estimator with a
lower sampling variance than the OLS estimator, but it would either be non-linear or biased or
both! So there is a trade-off between bias and variance in the choice of the estimator.

You might also like