Вы находитесь на странице: 1из 2

> # Introduction to R

> # Copyright 2014 by Michael Grogan


> # Tools -> Install Packages
>
> # Set working directory to where csv file is located
> setwd("C:\\Users\\Michael Grogan\\Documents\\Computer Programming\\R")
>
> # Read the data
> mydata<- read.csv("C:\\Users\\Michael Grogan\\Documents\\Computer Programming\
\R\\dividend.csv")
> attach(mydata)
The following objects are masked from mydata (position 5):
divyield, payout, return, years
>
> # OLS regression - return (dependent variable) and payout + divyield + years (
independent variables)
> olsreg <- lm(return ~ payout + divyield + years)
> summary(olsreg)
Call:
lm(formula = return ~ payout + divyield + years)
Residuals:
Min 1Q Median 3Q Max
-56.294 -16.177 -3.805 15.145 67.324
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 98.134359 19.625852 5.000 7.73e-06 ***
payout 0.052938 0.139074 0.381 0.70511
divyield -16.590832 4.409612 -3.762 0.00045 ***
years 0.009375 0.420345 0.022 0.98230
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 29.23 on 49 degrees of freedom
Multiple R-squared: 0.2401, Adjusted R-squared: 0.1936
F-statistic: 5.161 on 3 and 49 DF, p-value: 0.003524
> # summary(lm(return ~ payout + divyield + years))
>
> # Plotting data
> plot (return ~ payout + divyield + years)
Hit <Return> to see next plot: olsreg1 <- lm(return ~ payout + divyield + years)
Hit <Return> to see next plot: abline(olsreg1)
Hit <Return> to see next plot:
> # Redefining variables
> Y <- cbind(return)
> X <- cbind(payout, divyield, years)
> summary(Y)
return
Min. : 19.96
1st Qu.: 34.83
Median : 58.59
Mean : 61.75
3rd Qu.: 76.56
Max. :148.20
> summary(X)
payout divyield years
Min. : 0.00 Min. :0.600 Min. : 9.00
1st Qu.: 1.00 1st Qu.:1.690 1st Qu.:32.00
Median : 40.00 Median :2.150 Median :40.00
Mean : 38.38 Mean :2.338 Mean :39.87
3rd Qu.: 59.00 3rd Qu.:2.910 3rd Qu.:46.00
Max. :142.00 Max. :5.340 Max. :58.00
> olsreg <- lm(return ~ payout + divyield + years)
> summary(olsreg)
Call:
lm(formula = return ~ payout + divyield + years)
Residuals:
Min 1Q Median 3Q Max
-56.294 -16.177 -3.805 15.145 67.324
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 98.134359 19.625852 5.000 7.73e-06 ***
payout 0.052938 0.139074 0.381 0.70511
divyield -16.590832 4.409612 -3.762 0.00045 ***
years 0.009375 0.420345 0.022 0.98230
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 29.23 on 49 degrees of freedom
Multiple R-squared: 0.2401, Adjusted R-squared: 0.1936
F-statistic: 5.161 on 3 and 49 DF, p-value: 0.003524
>
> # Test for Heteroscedasticity: Breusch-Pagan Test/Test for Autocorrelation: Du
rbin-Watson Test
>
> library(lmtest)
> bptest(Y ~ X)
studentized Breusch-Pagan test
data: Y ~ X
BP = 9.4067, df = 3, p-value = 0.02435
> dwtest(Y ~ X)
Durbin-Watson test
data: Y ~ X
DW = 1.5216, p-value = 0.02519
alternative hypothesis: true autocorrelation is greater than 0

Вам также может понравиться