Introduction To R Program and Output
Introduction To R Program and Output
# Descriptive statistics
summary(mpg)
sd(mpg)
length(mpg)
summary(price)
sd(price)
# Frequency tables
table(make)
table (make, foreign)
# OLS regression - mpg (dependent variable) and weight, length and foreign
(independent variables)
olsreg <- lm(mpg ~ weight + length + foreign)
summary(olsreg)
# summary(lm(mpg ~ weight + length + foreign))
# Plotting data
plot (mpg ~ weight)
olsreg1 <- lm(mpg ~ weight)
abline(olsreg1)
# Redefining variables
Y <- cbind(mpg)
X <- cbind(weight, length, foreign)
summary(Y)
summary(X)
olsreg <- lm(Y ~ X)
summary(olsreg)
data: mpg
t = 0.9893, df = 25, p-value = 0.332
alternative hypothesis: true mean is not equal to 20
95 percent confidence interval:
19.00148 22.84467
sample estimates:
mean of x
20.92308
>
> # ANOVA for equality of means for two groups
> anova(lm(mpg ~ factor(foreign)))
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
factor(foreign) 1 90.69 90.688 4.5806 0.0427 *
Residuals 24 475.16 19.798
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # OLS regression - mpg (dependent variable) and weight, length and foreign
(independent variables)
> olsreg <- lm(mpg ~ weight + length + foreign)
> summary(olsreg)
Call:
lm(formula = mpg ~ weight + length + foreign)
Residuals:
Min 1Q Median 3Q Max
-4.3902 -1.2734 -0.2991 0.7241 8.5203
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 44.968582 9.322678 4.824 8.08e-05 ***
weight -0.005008 0.002188 -2.289 0.032 *
length -0.043056 0.076926 -0.560 0.581
foreign -1.269211 1.632134 -0.778 0.445
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Call:
lm(formula = Y ~ X)
Residuals:
Min 1Q Median 3Q Max
-4.3902 -1.2734 -0.2991 0.7241 8.5203
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 44.968582 9.322678 4.824 8.08e-05 ***
Xweight -0.005008 0.002188 -2.289 0.032 *
Xlength -0.043056 0.076926 -0.560 0.581
Xforeign -1.269211 1.632134 -0.778 0.445
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # Install and use packages
> # install.packages("plm")
> # library(plm)