Professional Documents
Culture Documents
# ngay 23/9/2022
# Chapter 2 p64
#Exer c2
dCEO <- read.csv(file = 'E:/ATUYET/ceosal2.csv' , header = F)
# header: lay dong dau tien trong file excel lam ten cot YES---T, NO---F
# file: duong dan file can doc va ten file.csv, ' '
# (i) Find the average salary and the average tenure in the sample.
mean(dCEO$V1)
#(ii) How many CEOs are in their first year as CEO (that is, ceoten 5 0)?
# What is the longest tenure as a CEO?
subset(dCEO, V6==0)
# subset: tao subset
# subset(tendata, dkien LOC)
subset(dCEO, v6==37)
# (iii) Estimate the simple regression model
# log(salary) = b0 + b1*ceoten + u,
# and report your results in the usual form.
# delta(ceoten)= +1
# delta(log(salary)) = b1_hat*delta(ceoten) = 0.009724*(+1) = 0.009724 = (0.9724%)
# chapter 2 C3 p64
#30/9/2022
# Chapter 2 c4 p64
dWAGE <- read.csv(file = 'E:/ATUYET/wage2.csv', header = F)
# Chapter 3 c1 p110
# bwght = b0 + b1*cigs + b2*faminc + u
dBWGHT <- read.csv(file = 'E:/ATUYET/bwght.csv', header = F)
# (i) What is the most likely sign for b2?
# La positive vi khi thu nhap cao thi chat luong do bo tang len, dan den ba bau co
nhieu suc khoe
# can nang cua tre so sinh cung se tang len
# (iii) Now, estimate the equation with and without faminc, using the data in
BWGHT.RAW.
# Report the results in equation form, including the sample size and R-squared.
# Discuss your results, focusing on whether adding faminc substantially changes the
estimated effect of cigs on bwght.
regC1.3.1 <- lm(V4 ~ V10, data=dBWGHT)
summary(regC1.3.1)
regC1.3.2 <- lm(V4 ~ V10+V1, data=dBWGHT)
summary(regC1.3.2)
cor(dBWGHT$V1,dBWGHT$V10)
lm(V1~V10, data=dBWGHT)
# b1_cigs^
# b1_cigs~ =b1_cigs^ + sig1^*b2
#07/10/2022
#Chapter 3 C2 p110
#price = b0 + b1*sqrft + b2*bdrms + u
dprice <- read.csv(file = 'D:/ATuyet/ATUYET/hprice1.csv', header = F)
# (ii) What is the estimated increase in price for a house with one more bedroom,
holding square footage constant?
# b0^ = -19.31500 , b1^ = 15.2
# delta(bdrms) = +1
# delta(price_hat)= b1^*delta(IQ)= 15.2*(+1)=15.2
# The house price increases by $15,200 with one more bedroom
# What is the estimated increase in price for a house with an additional bedroom
that
# is 140 square feet in size? Compare this to your answer in part (ii)
# b0^ = -19.31500 , b1^ = 0.13 , b2^ = 15.2
# delta(bdrms) = +1
# delta(sqrft) = +140
# delta(price_hat)= b1^*sqrft + b2^*bdrms = (+1)*15.2+(+140)*0.13 = 33.4
# The house price increases by $33,400 with one more bedroom that is 140 square
feet in size.
# Khi chi tang so luong phong ngu thi gia nha tang thap hon khi tang so luong phong
ngu và dien tich
# The first house in the sample has sqrft = 2,438 and bdrms = 4
# Find the predicted selling price for this house from the OLS regression line
# price^= -19.315+2438*0.13+4*15.2 = 358.425 (thousand of dollars)
# The actual selling price of the first house in the sample was $300,000 (so price
= 300).
# Find the residual for this house.
# Does it suggest that the buyer underpaid or overpaid for the house?
# the residual= Ui^ = Yi - Yi^ = 300-358.425 = -58.425
# the buyer should underpaid
# Chapter 3, C3 p111
dCEO2 <- read.csv(file = 'D:/ATUYET/ceosal2.csv', header = F)
# (i) Estimate a model relating annual salary to firm sales and market value.
# Make the model of the constant elasticity variety for both independent variables.
# Write the results out in equation form
# (iv) Find the sample correlation coefficient between the variables log(mktval)
and profits.
# Are these variables highly correlated? What does this say about the OLS
estimators
cor(dCEO2$V12, dCEO2$V8)
# correlation coefficient between the variables log(mktval) and profits is 0.77
# He so tuong quan cao nhung khong anh huong vi pham gia thiet so 3 (da cong tuyen)
#21/10/2022
#Chapter 3 C6-p112
dwage2 <- read.csv(file = 'D:/ATUYET/wage2.csv', header = F)
# (i) Run a simple regression of IQ on educ to obtain the slope coefficient,
say,δ1.
regC3.6.1 <- lm(V3 ~ V5, data=dwage2)
summary(regC3.6.1)
# δ1~ = 3.5338
# (ii) Run the simple regression of log(wage) on educ, and obtain the slope
coefficient
regC3.6.2 <- lm(V17 ~ V5, data=dwage2)
summary(regC3.6.2)
# b1~ = 0.059839
#Chapter 4 C1-p164
# voteA = b0 + b1log(expendA) + b2log(expendB) + b3 prtystrA + u
# (iv) Estimate a model that directly gives the t statistic for testing the
hypothesis in part (ii).
# What do you conclude? (Use a two-sided alternative.)
# voteA = b0 + b1log(expendA) + b2log(expendB) + b3 prtystrA + u (1)
# theta = b1 + b2 => b1 = theta - b2 , insert to (1)
# H0 : b2 + b1 = 0; H1 : b2 + b1 =/= 0
# H0 : theta = 0; H1 : theta =/= 0
# voteA = b0 + (theta - b2)*log(expendA) + b2*log(expendB) + b3*prtystrA + u (1)
# voteA = b0 + theta*log(expendA) + b2*[log(expendB) - log(expendA)] + b3*prtystrA
+ u (1)
dvote1$newexp <- dvote1$V8 - dvote1$V9
regC4.1.4 <- lm(V4 ~ V8 + newexp + V7, data=dvote1)
summary(regC4.1.4)
# voteA = 45.07893 - 0.53210*log(expendA) + 6.61542*[log(expendB) - log(expendA)] +
0.15196*prtystrA + u (1)
# t_value = 0.998 < 1.65 (DF > 120) tai muc y nghia 10% => ACCEPT H0
# b2 + b1 = 0 => b1= -b2
# a 1% increase in A’s expenditures is offset by a 1% increase in B’s expenditures