5.
Statistician is interested in regressing the returns (Y) of business on the number of branches
(X1) and the age of business X2. The statistician collected the relevant data form 10 big
companies as shown in the table below
Y 61 47 75 63 79 75 67 47 71 84
X1 19 19 10 17 19 12 18 10 15 15
X2 11 4 8 12 5 12 6 14 13 11
Write a well commented R program that does the following
(i) Reads in data [3 marks]
#creating a data frame
Y<-c(61,47,75,63,79,75,67,47,71,84)
X1<-c(19,19,10,17,19,12,18,10,15,15)
X2<- c(11,4,8,12,5,12,6,14,13,11)
Data<- [Link](Y,X1,X2)
(ii) Estimates the parameters β1 and β2 in the regression model Y = β0 + β1x1 + β2x2 +
e where e ∼ N (0,σ2) [3 marks]
#Fitting a linear regression model
model1<- lm(Y~X1+X2, data= Data)
beta0<- coef(model1)[1]
#Estimates of parameters β1 and β2
beta1<- coef(model1)[2]
beta2<- coef(model1)[3]
#print(model1$coefficients)
(iii) Estimates the parameters a, b and c in the regression model Y = a + b ∗ x1 + exp(c ∗
x2) + e where e ∼ N (0,σ2) [3 marks]
#Library in use
library([Link])
# Fitting a nonlinear model
model2<-nlsML(Y~a+b*X1+exp(c*X2),data=Data,start=list(a=1,b=1,c=1))
# Extraction of the parameter estimates
a<- coef(model2)[1]
b<- coef(model2)[2]
c<- coef(model2)[3]
#print(model2$coefficients)
#Display estimates a, b and c
cat("Estimated a, b and c\n","a = ",a, "b = ",b,"c = ",c)
(iv) Determine the p-values of the estimated parameters in the part (ii) and (iii) above [3
marks]
#p-values of the estimated parameters in the part (ii)
s_model1<-summary(model1)
s_model1
P_values_model1<- s_model1$coefficients[,4]
P_values_model1
#p-values of the estimated parameters in the part (iii)
s_model2<- summary(model2)
s_model2
P_values_model2<- s_model2$coefficients[,4]
P_values_model2