Vous êtes sur la page 1sur 7

ASM L1

> #Input the data

> Advertising <- c(1, 2, 3, 4, 5)

> Sales <- c(1, 1, 2, 2, 4)

>

> #Simple scatter plot , col = colour, pch = 19 means circle, cex = size

> plot(Advertising, Sales, xlab = "Advertising", ylab= "Sales", main="Sales vs. Advertising", xlim = c(0, 6.5),
ylim = c(0, 5), col= "red", pch = 19, cex = 1.5)

>

> #Use lm() function to fit a linear regression

> lm(formula = Sales~Advertising)

Call:

lm(formula = Sales ~ Advertising)

Coefficients:
(Intercept) Advertising

-0.1 0.7

>

> #Input the data

> x<- c(8, 10, 12, 14, 16)

> y<- c(9, 13, 14, 15, 19)

>

> #Compute the correlations and round the results to 4 significant digits

> round(cor(x,y),4)

[1] 0.9648

>

> #Input the data

> Advertising <- c(1, 2, 3, 4, 5)

> Sales <- c(1, 1, 2, 2, 4)

>

> #fit lm() fuction to a linear regression and call it by using summary (XXX.Reg)

> SA.Reg<-lm(formula = Sales~Advertising)

> summary(SA.Reg)

Call:

lm(formula = Sales ~ Advertising)

Residuals:

1 2 3 4 5

4.000e-01 -3.000e-01 -3.886e-16 -7.000e-01 6.000e-01

Coefficients:

Estimate Std. Error t value Pr(>|t|)


(Intercept) -0.1000 0.6351 -0.157 0.8849

Advertising 0.7000 0.1915 3.656 0.0354 *

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.6055 on 3 degrees of freedom

Multiple R-squared: 0.8167, Adjusted R-squared: 0.7556

F-statistic: 13.36 on 1 and 3 DF, p-value: 0.03535

>#Residuals means distance btw real and expected observation

>#Coefficients: all about beta

>#Residual std error - quality of a linear regression fit

>#R-square – how well model fit the actual data

>#F-statistic – Is there a relationship btw predictor and response variable

> #Input the data

> Age<-c(5, 10, 12, 14, 15)

> Price <- c(500, 400, 300, 200, 100)

>

> #Compute the confidence interval for the parameters

> PA.Reg <- lm(formula = Price~Age)

> summary(PA.Reg)

Call:

lm(formula = Price ~ Age)

Residuals:

1 2 3 4 5

-36.943 54.140 30.573 7.006 -54.777


Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 728.02 77.88 9.348 0.00259 **

Age -38.22 6.63 -5.765 0.01038 *

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 52.54 on 3 degrees of freedom

Multiple R-squared: 0.9172, Adjusted R-squared: 0.8896

F-statistic: 33.23 on 1 and 3 DF, p-value: 0.01038

> confint(PA.Reg,level=0.95)

2.5 % 97.5 %

(Intercept) 480.17941 975.8715

Age -59.31462 -17.1185

>

> #Input the data

> Q1 <- c(0, 2, 4, 6, 8)

> Q2 <- c(6, 5, 8, 7, 9)

>

> #Use lm() to fit a regression line

> Quiz.Reg <- lm(formula = Q2~Q1)

>

> #Input the new data xh

> ND <- data.frame(Q1=c(6))

>

> predict(object = Quiz.Reg, newdata = ND, se.fit = TRUE, interval = c("confidence"), level = 0.95)

$fit

fit lwr upr


1 7.8 5.890532 9.709468

$se.fit

[1] 0.6

$df

[1] 3

$residual.scale

[1] 1.095445

>#$se.fit is the standard error of expected estimated value of Yh

> predict(object = Quiz.Reg, newdata = ND, se.fit = TRUE, interval = c("prediction"), level = 0.95)

$fit

fit lwr upr

1 7.8 3.825126 11.77487

$se.fit

[1] 0.6

$df

[1] 3

$residual.scale

[1] 1.095445

>#$se.fit is not std err for prediction interval

>#3.825126 11.77487 from $fit is the prediction interval

> #Input the data


> Q1 <- c(0, 2, 4, 6, 8)

> Q2 <- c(6, 5, 8, 7, 9)

>

> #Use lm() to fit a regression line

> Quiz.Reg <- lm(formula = Q2~Q1)

> summary(Quiz.Reg)

Call:

lm(formula = Q2 ~ Q1)

Residuals:

1 2 3 4 5

0.6 -1.2 1.0 -0.8 0.4

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 5.4000 0.8485 6.364 0.00785 **

Q1 0.4000 0.1732 2.309 0.10409

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.095 on 3 degrees of freedom

Multiple R-squared: 0.64, Adjusted R-squared: 0.52

F-statistic: 5.333 on 1 and 3 DF, p-value: 0.1041

>

> #To obtain the ANOVA table

> anova(Quiz.Reg)

Analysis of Variance Table


Response: Q2

Df Sum Sq Mean Sq F value Pr(>F)

Q1 1 6.4 6.4 5.3333 0.1041

Residuals 3 3.6 1.2

> #2.309^2 = 5.3333

>#from ANOVA , Q1 is regression, Residuals is error

Vous aimerez peut-être aussi