Enter the following commands in R:
a=rnorm(1)
b=rnorm(1,2,1)
sigma_b=runif(1)
x=runif(100)
y=a*x+b+rnorm(100,0,sigma_b)
This produces 100 observations corresponding to a linear model with the equation \[Y=-1.8712155 X+1.2058265.\]
r=cor(x,y)
r
## [1] -0.9184906
Please proceed to the following sections only if the linear relationship is confirmed by the scatter plot and the correlation value. ## Estimated regression line equation
res=lm(y~x)
summary(res)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.61012 -0.17202 0.02278 0.13010 0.49507
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.23626 0.04770 25.92 <2e-16 ***
## x -1.91467 0.08327 -22.99 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2261 on 98 degrees of freedom
## Multiple R-squared: 0.8436, Adjusted R-squared: 0.842
## F-statistic: 528.7 on 1 and 98 DF, p-value: < 2.2e-16
a0=res$coefficients[2]
b0=res$coefficients[1]
Estimated regression line: \[Y=-1.9146655 X+1.2362642.\] Coefficient of determination: R2=0.843625 (percentage of total variance explained by the linear model)
The true line (in red) and its estimate (in blue).
When X=0.2, the mean value of Y is estimated at 0.8533311. Envoyer des commentaires