knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)

Estima el modelo:

\[ Testscore = \beta_0 + \beta_1StudentTeacherRatio +u_i \]

  1. Leer el archivo:
  1. Extraemos los datos que nos interesan:

  2. Para realizar los cálculos necesitamos la media de \(X\) y de \(Y\) así que las calculamos:

## [1] 24.02282
## [1] 753.8542
  1. Ahora podemos estimar \(\hat{\beta}_1\) por medio de la fórmula:

\[ \hat{\beta}_1 = \frac{\sum (X_i - \bar{X})(Y_i - \bar{Y})}{\sum (X_i - \bar{X})^2} \]

Primero la parte de arriba

## [1] -6007.325

Ahora la parte de abajo:

## [1] 6322.869

Calculamos \(\hat{\beta}_1\):

## [1] -0.9500948
  1. Para calcular \(\hat{\beta}_0\) usamos la fórmula:

\[ \hat{\beta}_0 = \bar{Y} - \hat{\beta}_1 \bar{X} \]

## [1] 776.6782
## [1] 776.6782
  1. Por último calculamos los residuos con la fórmula:

\[\hat{u}_i = Y_i - \hat{Y}_i\]

##   [1]  -23.38811591    3.50175773  -47.24100846  -66.81782067  -18.94089816
##   [6]   51.74030706  -19.04800143  -62.12939330  -74.16865652  108.82230250
##  [11]  -56.33027287  -28.93194682  -21.25574448   34.09141855   37.35303562
##  [16]   61.90995385  116.58622810  -66.66668057   65.84213852  -64.38067625
##  [21]  -12.19920358    6.08137369   77.91654551  -63.39271056   14.98910657
##  [26]   -4.07566644  -28.37354911   75.52367562  -62.84393588  136.73739222
##  [31]  -51.12565033  228.10949344   -4.51297611  -51.95590616  -43.71813482
##  [36]   12.08989879  -47.39702806   60.14621530  167.92018339    2.14277048
##  [41]  145.22756965   31.83359432   -4.52987857  -78.62540638  -81.72418525
##  [46]  -30.93277978  111.80328240   51.17417413  -40.40154017  -28.35076265
##  [51]  -32.46913302    7.17201953   52.26102615  103.09527995  -87.89931219
##  [56]   -5.82301742   -3.97570665  -17.60520976  -60.33479021    9.85749106
##  [61]  187.25988365    9.69651187   46.17041791   17.38148692   81.31776617
##  [66]  -16.48324168   56.60957457   31.36906300   24.65958992 -108.97455855
##  [71]   10.36775231  -20.85593005    1.72683286   57.61814545   24.10227968
##  [76]   13.53885872    2.31441475   32.88637957   23.36621252  -47.45380790
##  [81]   16.04367827  -38.85996271  119.38318228   -3.51696495   35.36404438
##  [86]  -53.03127416  -13.17581847  -15.95378777 -107.65531998    9.77333930
##  [91]   -8.16063565 -109.12543801   54.21546031   -8.24476872  120.26686823
##  [96]   46.25703235   29.12793552   -1.93187148   39.53736548   59.42590087
## [101]   66.37573586   -1.78967069  -81.52290434   23.61473960  -53.87852778
## [106]  104.18834340   25.55898885    9.54757805   11.53958785    6.55439756
## [111]  -39.74585393    6.48469132   20.75590915   16.96752199   91.42095225
## [116]   -3.53082332  -57.91427663    6.29342162  -37.01719058 -106.09544345
## [121]  -80.27424840  -22.47074519   68.69413445   28.10701695   12.81571377
## [126]   -8.07566644   47.66555694  -35.83641217   51.38978469  -59.07743928
## [131]  -82.18200102  -11.49102559  -15.51678745  128.44282748  -51.32487989
## [136]   28.63628695  -16.07559383   37.90146999  -97.54696984   26.22394806
## [141]  113.88747222  -84.77327748   21.41823093  -55.38792883   74.91950558
## [146]  -50.46250204  -66.40613810  -42.08269882  -29.73893143   70.09453640
## [151] -159.94192169   44.83028602  -55.03720816   -9.63494325    2.74344294
## [156]    8.40739624   53.38179529   21.42455561   96.85023405  -18.26585137
## [161]   -6.04928220   14.66804280  -15.02598751  -35.45553877  -72.22590708
## [166]  -25.30040499  -73.95881452   81.61154831  -57.61867091    0.96761992
## [171] -124.42897765  -37.17928182   19.23666403   12.14092193   88.16896519
## [176]   90.57841870   77.37417932  -10.30101871  -15.53435821  186.87426314
## [181]   14.52966640  -24.69999983  -80.14237905   77.97832990   17.68769286
## [186]   42.53427277  -50.51297611   28.14814848  120.34017907   52.42201830
## [191]   86.65653978   73.65083064   -0.77941395   14.56078425   30.20150044
## [196]   36.16498849   85.96651711   14.03124670  -31.43594342   47.54778810
## [201]  -64.29513130  -22.57541739  -45.43638777   15.49422557  -17.41983541
## [206]  -63.42825799  -41.29380768   34.76486039  -78.78285693  -34.04384328
## [211] -113.09392958   -3.34837166  -24.70570152   14.22424874   36.79209121
## [216]  153.31014531   -4.83279080  -75.19571440   -0.50784511  -57.03432368
## [221]   75.16539170  -97.58767863   21.31891801   29.42102415  -25.91251558
## [226]   -1.38670884   70.72386425 -122.65292537   11.95120992  -71.87832397
## [231]   91.81702069   20.20228669  -13.40331358   41.65281451  109.86178338
## [236]   56.70251172  -18.67475505  -40.70734874   49.70557721  -24.98014628
## [241]  -81.19696826   61.82420433  -27.13761377  -41.38107909   -3.51284790
## [246]   13.27852410   -0.35131938   15.20056305  -42.53648895  125.20259147
## [251]  -30.42802158 -120.28550592 -113.56069548   79.77838568  -94.63152791
## [256]  -25.00290337   39.23129085  -40.51880841   -7.06837598  -71.12004253
## [261]   -9.81958454  124.72898409   45.42894108   34.14788872  -12.92450760
## [266]   24.03238478  -55.26818736  -61.54038734  -80.33435197   42.12618568
## [271]  -72.14871955  -55.67572586  -11.67344846  -20.61264635    4.19417677
## [276]   17.62810076   78.05453432  -40.34430908  -88.64303980  -57.92583548
## [281]   -0.86645558  -24.89458421   78.32760152  -18.89795541   21.56188166
## [286]  -53.88936035  -77.25507244  -81.77710360   46.57582237 -122.16203487
## [291]   -9.88389575  -14.39002352  -19.20947365  -29.82628297  -13.74352408
## [296]  -27.53231277   41.00537969   -2.46272127    3.88659581  -62.83411305
## [301]   22.95469403   18.11884128   76.08364063  -35.93482048   47.36588701
## [306]   33.14190178   92.99279875  -61.57197488  134.58917441 -107.42617715
## [311]  -70.57545839  -50.34037866  -81.67219581  -73.73911334   48.59038516
## [316]    2.54703842   -8.57802419  -75.76932717  -58.14697245  -41.31033322
## [321]   64.42254753   17.11968947  -77.38001598  -18.19484495   44.13781507
## [326] -110.27601332   28.36283871  126.97435417   -1.89373307  -96.62721051
## [331]  -86.15780592   39.46382843   66.16561748  -51.87410202  -52.08953778
## [336]   15.86822366   64.60721004  -67.48495190  -17.96616205   53.52397867
## [341]  141.23054869  -68.02121064  -50.34714390  -24.21617155   -3.66554529
## [346]  -16.17529575   33.22448660    6.55753706  -82.94173512   13.69645084
## [351]   26.65692213   96.24094285  -67.93209772  -28.50140169  -14.91961933
## [356]   -1.11831607  -59.01447685  -81.34271954 -117.44146268  -19.03067655
## [361]  115.27721303   14.11565176   20.84337300  -40.80685857  -94.71753853
## [366]  -59.77700368  133.98661419  -28.97861010  -63.10787710  -42.98053368
## [371]   43.77435109  -35.56933486  -46.16611910  -49.19608136  -11.57380950
## [376]   47.65905374  -36.46600959  -89.04227811   42.28279853   50.46990922
## [381]   17.83394345   16.78918661  -76.31650284  -40.87779961   45.87812806
## [386]  -51.87574687   47.91575071   78.09837544   33.09160512  -18.70713954
## [391]  -42.93405482   28.69216709 -102.86496010    0.05216615   25.87558393
## [396]  -46.89744585   31.26822544  -41.61751745   77.20568326  -57.79049265
## [401]    4.66914254  -60.99202691  -39.82641660    6.11211732  -17.59386159
## [406]  -15.99138377  108.76300670   28.96223324   47.74927395  -26.12369273
## [411]  -62.30694799  -67.15199785 -121.45649359   73.93330696  -61.39308598
## [416]  -32.10429284  -12.38912484  109.15324884  -26.68473785   13.46018252
## [421]   10.38535051  -27.74646965  136.52384506   -2.76784751  -79.05248713
## [426]    2.81005435 -100.86276685   20.36409291 -104.03896165   16.54209142
## [431]  -71.89800736   51.98630449  -25.18886101   36.24271104   16.94477821
## [436]  -43.56744904  -34.34004802  -69.29682647    1.13700098   -5.17362787
## [441]   42.30547649   55.54282814   26.65259259  -13.02117875   30.53713214
## [446]   89.23739531  -33.13272013    6.94225432    3.84817475   41.22608957
## [451]  -49.87850886   54.74408670  -10.93008323  -43.22555841   41.65475600
## [456]  111.95136763  -54.26075395   44.70812598   82.01031516   29.22024053
## [461]  -58.81308105   -2.23976868   35.46297956   47.16488479  -45.23133224
## [466]   19.75136297   -1.20889714   44.71274305  -32.32021430   42.99301433
## [471]   38.82413172  -14.57608492   22.55563561   32.32191534   20.59060161
## [476]  136.64254039   16.07305974  -93.86565651   49.29441048   60.08324915
## [481]  -10.43721507 -128.72097770    7.65725313  -20.16090542   96.97446960
## [486]  108.11252487  -61.25586545  -14.17609453  -84.54010738   62.74763506
## [491]   53.79035344  -17.62921152  -48.68128924   37.06782176   49.88532131
## [496]  -73.98468973   -0.26878976    2.27345057  106.31631484  -11.94097297
## [1] -3.365952e-14
  1. Para validar nuestros resultados, podemos comparar con lo que nos da R al ajustar el modelo de regresión lineal simple:
## 
## Call:
## lm(formula = testscore ~ str_s, data = datos)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -159.942  -43.042   -2.086   37.490  228.109 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 776.6782    18.4064  42.196   <2e-16 ***
## str_s        -0.9501     0.7579  -1.254    0.211    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 60.27 on 498 degrees of freedom
## Multiple R-squared:  0.003145,   Adjusted R-squared:  0.001144 
## F-statistic: 1.571 on 1 and 498 DF,  p-value: 0.2106
  1. Ejercicio:
  1. Calcula el error estándar.
## [1] 60.26901
  1. Calcula la \(R^2\).
## [1] 0.003145303
  1. Grafica los datos con la pendiente estimada
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.