#dataset GaltonFamilies se nalazi u paketu 
library(HistData)

GaltonFamilies[1:10,]
##    family father mother midparentHeight children childNum gender childHeight
## 1     001   78.5   67.0           75.43        4        1   male        73.2
## 2     001   78.5   67.0           75.43        4        2 female        69.2
## 3     001   78.5   67.0           75.43        4        3 female        69.0
## 4     001   78.5   67.0           75.43        4        4 female        69.0
## 5     002   75.5   66.5           73.66        4        1   male        73.5
## 6     002   75.5   66.5           73.66        4        2   male        72.5
## 7     002   75.5   66.5           73.66        4        3 female        65.5
## 8     002   75.5   66.5           73.66        4        4 female        65.5
## 9     003   75.0   64.0           72.06        2        1   male        71.0
## 10    003   75.0   64.0           72.06        2        2 female        68.0
dim(GaltonFamilies)
## [1] 934   8
#SIMPLE REGRESSION ----
#1. izrada modela
lmmodel=lm(childHeight ~ midparentHeight,data=GaltonFamilies)
summary (lmmodel)
## 
## Call:
## lm(formula = childHeight ~ midparentHeight, data = GaltonFamilies)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.9570 -2.6989 -0.2155  2.7961 11.6848 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     22.63624    4.26511   5.307 1.39e-07 ***
## midparentHeight  0.63736    0.06161  10.345  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.392 on 932 degrees of freedom
## Multiple R-squared:  0.103,  Adjusted R-squared:  0.102 
## F-statistic:   107 on 1 and 932 DF,  p-value: < 2.2e-16
# the fitted model is: Predicted child height=22.6362+.6374⋅Midparent height.

library(ggplot2)
ggplot(GaltonFamilies, aes(x=midparentHeight, y=childHeight)) + 
  geom_point() + 
  geom_smooth() #linearna pozitivna veza
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(GaltonFamilies, aes(x=midparentHeight, y=childHeight)) + 
  geom_point() + 
  geom_smooth(method = "lm", se=F) #linearna pozitivna veza  
## `geom_smooth()` using formula = 'y ~ x'

   #this is withoud SE, but we know from the previous slide thad se is small, because we have large sample size and small variability

confint(lmmodel)#interval povjerenja
##                      2.5 %     97.5 %
## (Intercept)     14.2659135 31.0065676
## midparentHeight  0.5164552  0.7582666
confint(lmmodel, level = 0.9) # manji su intervali jer trazimo i manji nivo pouzdanosti 90 versus 95
##                        5 %       95 %
## (Intercept)     15.6137829 29.6586982
## midparentHeight  0.5359246  0.7387972
lmmodel$coefficients
##     (Intercept) midparentHeight 
##      22.6362405       0.6373609
#2. interpretacija modela 

#interpretacija: to znači da u prosjeku za svaki dodatni centimetar 
#prosjecnih visina roditelja, ocekujemo da ce se visina djeteta povecati 
#u prosjeku za od 0.637, odnosno kretace se u intervalu od 0.516 do 0.758 
# “A unit change in x1 will produce a change of ˆβ1 in the response
##intersept je 22.63, sto znaci kad je prosjek visine roditelja nula, 
#visina djeteta u prosjeku je 22.636incha. Ovo naravno nema smisla

## t stat= coeff/st.error
#kod simple posmatrmo Rsquare a kod multiple adjRsq te p/value

#3 verifikacija modela

#DIAGNOSTIC PLOT----

#Homeoscesadicity
##**Residuals versus fittet plot----
par(mar = c(4, 4, 2, 2)) # Adjust the numbers as needed

plot (lmmodel,which=1) #which=1 odnosi se na razlicite vrste plotova, a ima ih 6. jedan oyna;ava residuals vs fitted

#ovjde istrazhujemo homoscedasticity or heteroscedasity
#gledamo linear concept of data. Ako je linija zakrivljena nije linearni model.
#ovaj plot mora biti homeoscedastic.
#na ovom plotu vidimo i outlayere. Izbaci nam broj reda i onda odlucimo da li trebmo da ga izbacimo.
#moramo imati argument za to

lmmodel$coefficients
##     (Intercept) midparentHeight 
##      22.6362405       0.6373609
confint(lmmodel, level=0.99) # ako zelimo da promijenimo nivo pouzdanosti
##                      0.5 %     99.5 %
## (Intercept)     11.6275088 33.6449723
## midparentHeight  0.4783446  0.7963772
##**Cook's distance ----

plot(lmmodel,which=4,id.n = 5) #id.n=5  znači da dobijemo pet rednih brojeva koji su najudaljeniji od fitted value)

##**Other plots---- 

##qq plot
plot(lmmodel,which=2)

##standardized residuals versus fitted values
plot(lmmodel,which=3) 

## calculate stand residuals 

# Extract standardized residuals
standardized_residuals <- rstandard(lmmodel)

# Display standardized residuals
standardized_residuals
##            1            2            3            4            5            6 
##  0.738574554 -0.449022391 -0.508402238 -0.508402238  1.158927030  0.862961935 
##            7            8            9           10           11           12 
## -1.208793727 -1.208793727  0.719436074 -0.166738752  0.571740269 -0.019042947 
##           13           14           15           16           17           18 
## -0.462130360 -1.200609381 -1.643696794  1.571878724  0.686894108  0.391899236 
##           19           20           21           22           23           24 
## -0.050593072 -1.230572560 -1.230572560 -0.036254786  2.035686182  1.295707265 
##           25           26           27           28           29           30 
##  0.999715698  0.999715698  0.259736781 -1.664208404  0.412090334 -0.327071124 
##           31           32           33           34           35           36 
## -0.918400291 -0.914608975 -0.963879534  1.901027902  0.720735044  0.130588615 
##           37           38           39           40           41           42 
## -0.164484600 -0.164484600 -0.459557815 -1.197240851 -1.344777459 -0.652957353 
##           43           44           45           46           47           48 
##  0.603099864 -2.057153039 -0.283651104 -0.579234759  0.653770724  0.506023049 
##           49           50           51           52           53           54 
## -0.616859276  1.100992468  0.658082020  0.569499931  0.569499931  0.274226299 
##           55           56           57           58           59           60 
##  0.126589483 -0.523012506 -1.113559770 -1.408833401  1.742001173  1.446787099 
##           61           62           63           64           65           66 
##  1.003965989 -1.652960670 -0.472104377 -1.712003485 -0.568807784 -1.011550283 
##           67           68           69           70           71           72 
## -1.159131116 -1.459719678  1.079489870  1.020301208  0.931518215  0.132471276 
##           73           74           75           76           77           78 
## -0.163472034 -0.311443689 -0.459415344 -1.051301965  1.186786429 -0.143401659 
##           79           80           81           82           83           84 
## -0.291200336  1.287775866  0.696924239 -0.484779017  1.844038584  0.751913362 
##           85           86           87           88           89           90 
##  0.456744383 -0.576347044 -0.723931534 -0.871516023 -0.871516023 -0.774866740 
##           91           92           93           94           95           96 
## -0.474619743 -1.359870122  0.954631611  0.954631611  0.512091964 -0.668013761 
##           97           98           99          100          101          102 
## -1.258066624  0.512091964 -0.077960898 -1.258066624  1.692197690 -0.077960898 
##          103          104          105          106          107          108 
##  0.954631611  0.807118396 -0.225474114 -1.258066624 -0.028232810 -0.116757062 
##          109          110          111          112          113          114 
## -0.264297482 -0.861416029  1.156601894  0.861594349  0.861594349 -0.318435834 
##          115          116          117          118          119          120 
## -0.613443380 -0.613443380  2.088562417  1.498566728  0.613573194  0.171076427 
##          121          122          123          124          125          126 
## -1.008914951  1.498566728  1.351067806  1.351067806  0.908571039  0.318575349 
##          127          128          129          130          131          132 
## -0.258388695  2.657361661  1.474916826  1.179305617  0.883694409 -0.594361635 
##          133          134          135          136          137          138 
##  1.440357941  1.381299410  0.200128795 -0.390456512  0.596818809 -0.288710538 
##          139          140          141          142          143          144 
## -0.288710538 -0.436298763  0.596818809  0.301642360  0.154054136 -0.583886987 
##          145          146          147          148          149          150 
## -1.026651661 -1.469416335  0.891995258 -0.288710538  2.367877504  1.187171708 
##          151          152          153          154          155          156 
##  0.891995258 -0.583886987 -0.583886987  0.729394398  1.237769146  1.237769146 
##          157          158          159          160          161          162 
##  0.942640079  0.352381946 -0.533005254 -0.828134321  1.486091932 -0.816255085 
##          163          164          165          166          167          168 
##  0.208577207 -0.027523465  0.108026151  0.108026151 -1.662506749  0.209546308 
##          169          170          171          172          173          174 
##  0.209546308  0.062031468 -0.232998212 -0.232998212 -0.380513052 -0.528027892 
##          175          176          177          178          179          180 
## -0.675542732  1.273383427  0.978283427  0.830733427 -0.202116571  0.209546308 
##          181          182          183          184          185          186 
##  0.209546308  0.209546308  1.292131164  0.997045046  0.701958927 -0.478385547 
##          187          188          189          190          191          192 
## -0.921014725 -0.921014725 -1.658730021  1.634067853 -1.611544737  0.144755912 
##          193          194          195          196          197          198 
## -0.740251669  1.145332876 -0.034710245 -0.329721026 -0.624731806 -1.067247977 
##          199          200          201          202          203          204 
##  1.196052713  0.901053726  0.901053726 -0.868940199 -0.573941212 -0.573941212 
##          205          206          207          208          209          210 
## -0.868940199 -1.163939187 -1.163939187  1.196052713  1.196052713  0.901053726 
##          211          212          213          214          215          216 
## -1.016439693  1.297573951  1.002577238 -0.619904684 -1.209898110 -1.504894823 
##          217          218          219          220          221          222 
##  1.592570665  1.150075595  1.150075595 -0.619904684 -1.947389892  0.951804219 
##          223          224          225          226          227          228 
## -0.818161574 -0.818161574 -0.818161574 -1.260653022  1.150075595  1.002577238 
##          229          230          231          232          233          234 
##  0.707580525  0.707580525 -0.177409614 -0.619904684 -0.767403040 -1.255977296 
##          235          236          237          238          239          240 
##  1.852922437  1.114742126  0.681915264  0.386465857  0.091016450  0.091016450 
##          241          242          243          244          245          246 
##  0.386465857  0.002381628 -0.204432957 -0.795331771 -1.386230585 -1.977129399 
##          247          248          249          250          251          252 
##  1.964421237  0.589308678  0.294124602 -0.591427625 -1.181795777 -2.362532081 
##          253          254          255          256          257          258 
## -0.148651511  1.525408017  1.230272381  1.230272381 -0.392973617  0.403892600 
##          259          260          261          262          263          264 
## -0.186378672 -0.392973617 -0.540541435 -0.540541435 -1.071785580 -1.219353398 
##          265          266          267          268          269          270 
##  1.335261023  0.804156804  0.450087324  0.302558374  0.155029425 -0.730144275 
##          271          272          273          274          275          276 
## -1.762846923 -1.910375873 -1.910375873  1.677192446  1.382159361  0.939609735 
##          277          278          279          280          281          282 
## -0.683072229 -0.683072229 -0.830588771 -1.273138398 -1.568171482 -0.093006061 
##          283          284          285          286          287          288 
## -0.683072229 -0.830588771 -1.420654940 -1.420654940  0.792093193  0.792093193 
##          289          290          291          292          293          294 
## -0.093006061 -0.683072229 -0.683072229 -1.273138398  3.447390952  2.267258614 
##          295          296          297          298          299          300 
##  1.087126277  0.497060108 -0.093006061 -0.476549070 -1.568171482  1.677192446 
##          301          302          303          304          305          306 
##  1.529675903 -0.683072229  0.497060108  0.497060108  1.412560850  0.822520331 
##          307          308          309          310          311          312 
##  0.438993993 -0.210050578 -0.505070838 -0.741087046 -0.800091098  1.100037027 
##          313          314          315          316          317          318 
##  0.893536827  0.303536254  0.008535968 -0.286464318 -0.581464604  0.008535968 
##          319          320          321          322          323          324 
##  0.893536827  0.303536254 -0.079964118 -0.433964461  1.463252318  0.873243175 
##          325          326          327          328          329          330 
## -1.339291114 -1.722797057 -2.047302086  2.026612593  0.699058878  0.551552910 
##          331          332          333          334          335          336 
##  0.256540974  0.256540974  0.256540974 -0.480988868 -0.628494836 -2.154464227 
##          337          338          339          340          341          342 
##  0.303536254 -0.581464604 -0.876464891 -1.466465463  1.188537113  0.893536827 
##          343          344          345          346          347          348 
##  0.893536827  0.893536827  0.746036684  0.451036398  0.598536541 -0.581464604 
##          349          350          351          352          353          354 
## -0.876464891  0.923982875  0.038994907 -0.403499077 -0.934491857 -1.081989852 
##          355          356          357          358          359          360 
## -1.288487044 -1.376985841 -1.730981028  0.948044195  0.505552592 -0.379430616 
##          361          362          363          364          365          366 
## -0.969419421  1.685530202  0.653049793  0.063060988 -0.674425018 -0.821922219 
##          367          368          369          370          371          372 
##  1.239278839  0.206797931  0.206797931 -0.973180249  0.405061931  0.110066024 
##          373          374          375          376          377          378 
## -0.863420470 -1.364913512  0.995053745 -0.037431930 -1.364913512 -1.659909419 
##          379          380          381          382          383          384 
##  1.639623134  1.049612570  0.902109928  0.902109928  0.459602005 -0.425413842 
##          385          386          387          388          389          390 
## -0.720419124 -1.015424406  1.203808026  0.908811894  0.761313827 -0.418670703 
##          391          392          393          394          395          396 
## -0.772666062 -0.920164128 -1.008662968  1.639623134  1.639623134 -1.900440253 
##          397          398          399          400          401          402 
##  1.552511743  0.313207599  0.414877904 -0.323005518 -0.175428834 -0.765735572 
##          403          404          405          406          407          408 
## -0.175428834 -0.175428834  1.896568425 -0.170730118 -0.613722663  2.095570388 
##          409          410          411          412          413          414 
##  0.323164223  0.323164223  0.027763196 -0.563038859  2.007344064  0.826350051 
##          415          416          417          418          419          420 
##  0.531101548 -0.649892465 -0.649892465 -0.797516716 -0.945140968 -0.945140968 
##          421          422          423          424          425          426 
## -1.240389471 -1.240389471 -1.087416570  1.669690894  1.374654249  1.286143256 
##          427          428          429          430          431          432 
##  1.227135927 -0.543083943 -0.690602266 -1.369186549 -1.428193878  1.138624933 
##          433          434          435          436          437          438 
##  1.079617604  0.784580959  2.188990050  1.893912047  1.303756041  0.270983030 
##          439          440          441          442          443          444 
##  0.784580959  0.342025992  0.194507669 -0.690602266 -1.280675556 -1.428193878 
##          445          446          447          448          449          450 
##  1.619073082  1.028943555  0.881411174  0.881411174 -0.151315497 -0.446380260 
##          451          452          453          454          455          456 
## -1.921704075  0.834487060 -0.050804376 -0.936095811 -1.083644384  1.028943555 
##          457          458          459          460          461          462 
##  0.291281648 -0.003783115 -0.446380260 -1.331574549 -1.331574549  1.032696666 
##          463          464          465          466          467          468 
##  1.032696666  0.885165448  0.885165448 -0.295084297 -0.590146733 -0.885209169 
##          469          470          471          472          473          474 
##  1.669690894  1.374654249  0.489544314  0.489544314 -0.248047298 -0.543083943 
##          475          476          477          478          479          480 
## -0.543083943 -0.690602266 -0.985638911  0.886020330  0.443517719  0.001015107 
##          481          482          483          484          485          486 
## -0.588988375 -0.883990116 -1.031490986 -1.768995339  0.801278231  0.299776541 
##          487          488          489          490          491          492 
## -2.060231412 -0.526226243 -0.732726939 -0.968727734 -2.060231412  1.470216209 
##          493          494          495          496          497          498 
## -0.063754225 -0.506245697 -0.948737168 -0.289190130  0.794124550  0.351598768 
##          499          500          501          502          503          504 
## -0.828469985  1.679176114  1.974193302  1.089141738  1.089141738 -0.680961391 
##          505          506          507          508          509          510 
## -0.090927014 -1.270995767  1.185823221  0.153313875 -0.141688795 -0.436691465 
##          511          512          513          514          515          516 
## -1.026696805 -1.321699476 -1.616702146  1.185823221 -0.820194936 -1.026696805 
##          517          518          519          520          521          522 
## -1.087814391  2.028884378  1.733843679  0.848721583  2.076085171  1.485950066 
##          523          524          525          526          527          528 
##  1.485950066  0.895814961 -0.874590354  1.733843679  1.143762282  0.494672744 
##          529          530          531          532          533          534 
## -0.272433072 -0.419953422 -0.478961561 -0.774002260 -0.774002260 -1.069042959 
##          535          536          537          538          539          540 
## -1.216563308 -1.364083658  1.435032865  1.139994051  1.139994051  0.844955237 
##          541          542          543          544          545          546 
## -0.777758240 -1.072797054 -1.072797054 -1.220316461  1.781017619  0.600747409 
##          547          548          549          550          551          552 
##  0.010612304  0.010612304  1.245584979  1.098031804  0.950478630 -0.820159468 
##          553          554          555          556          557          558 
## -1.115265818  0.702489904  0.702489904  0.554916707 -0.478095673 -0.773242068 
##          559          560          561          562          563          564 
## -0.773242068 -0.625668871 -1.068388462 -1.068388462  1.542498893  0.804364438 
##          565          566          567          568          569          570 
## -0.819531363  1.099618220  0.213856874 -1.055734389 -1.410038927  1.196203583 
##          571          572          573          574          575          576 
##  0.651894283 -2.096241681  1.615324779  1.025190521 -0.155077995  0.780824929 
##          577          578          579          580          581          582 
##  0.485786445  0.485786445  0.397274900  0.338267204  0.338267204  0.190747962 
##          583          584          585          586          587          588 
##  0.190747962  0.190747962 -0.340321309 -1.225436761  0.242521528 -0.199970616 
##          589          590          591          592          593          594 
## -0.250736142 -0.840727153  1.430738241  1.371739139  1.135742735 -0.398233895 
##          595          596          597          598          599          600 
## -0.103238389 -0.486732546 -1.578215917  1.569997962  1.569997962  0.390018910 
##          601          602          603          604          605          606 
## -0.199970616  0.697009143  0.343014894 -0.246975520 -0.246975520 -1.426956348 
##          607          608          609          610          611          612 
## -1.574453952 -1.721951555 -2.016946762  1.522995723  1.080502912  0.549511539 
##          613          614          615          616          617          618 
##  0.490512498  0.254516332 -0.836965934 -0.984463538 -1.131961141  1.376625500 
##          619          620          621          622          623          624 
##  0.491581734  0.491581734  0.196567145 -0.393462033 -0.688476622 -0.983491210 
##          625          626          627          628          629          630 
## -0.983491210 -1.278505799 -1.573520388 -0.098447444 -0.983491210 -0.245954738 
##          631          632          633          634          635          636 
## -1.278505799  1.435628418  1.435628418  0.786596323  0.639089028 -1.130998505 
##          637          638          639          640          641          642 
## -1.329213434  0.786596323  0.196567145 -0.098447444 -0.098447444 -0.098447444 
##          643          644          645          646          647          648 
## -0.393462033 -0.393462033 -0.393462033 -0.688476622 -0.983491210  1.330872480 
##          649          650          651          652          653          654 
##  1.183341057  0.593215365  0.003089673  0.003089673  0.003089673 -1.177161710 
##          655          656          657          658          659          660 
## -1.324693133  2.117931778  1.085376236  0.937868302 -0.242195174  0.298152519 
##          661          662          663          664          665          666 
##  1.183341057  0.593215365 -0.439504595 -0.587036018  1.478403903  0.593215365 
##          667          668          669          670          671          672 
##  0.003089673 -0.144441750 -0.291973172 -0.882098864 -1.177161710 -1.177161710 
##          673          674          675          676          677          678 
##  0.298152519  0.298152519  0.003089673 -0.587036018 -0.734567441 -1.472224556 
##          679          680          681          682          683          684 
##  0.554347672  1.183341057  1.113621906  0.818595193  0.228541767 -0.213998303 
##          685          686          687          688          689          690 
## -0.509025016  0.791616758  0.540471697 -1.230047868  0.201421462  0.944435417 
##          691          692          693          694          695          696 
##  0.796812994 -0.384166392 -0.531788815 -0.679411239  0.494462877 -0.282678676 
##          697          698          699          700          701          702 
## -1.168814921 -1.168814921 -1.464193669 -1.464193669 -1.464193669 -2.645708662 
##          703          704          705          706          707          708 
##  0.751146943  0.603457569  0.455768195 -1.464193669  1.249165448  1.001049188 
##          709          710          711          712          713          714 
##  0.114424993  0.025762573 -0.181116406 -0.181116406 -0.476657804  0.114424993 
##          715          716          717          718          719          720 
##  0.114424993 -0.772199203 -1.067740601  1.748664812  1.748664812  1.394671664 
##          721          722          723          724          725          726 
## -0.375294080 -0.965282661  1.216723588  1.857469871  1.267481021  1.178982694 
##          727          728          729          730          731          732 
##  0.972486596  0.677492171  0.382497746 -0.354988316 -1.387468803  0.985577142 
##          733          734          735          736          737          738 
##  0.336550422 -0.253473870 -0.400979942 -0.548486015 -0.843498161  1.023269845 
##          739          740          741          742          743          744 
##  0.728270112 -0.304228954 -0.304228954 -1.041728287  1.023269845  0.344770459 
##          745          746          747          748          749          750 
## -1.041728287 -1.926727487 -0.400979942 -1.286016379 -1.286016379  1.322033032 
##          751          752          753          754          755          756 
##  0.732032215 -0.742969830 -0.742969830 -1.037970239 -1.037970239 -1.037970239 
##          757          758          759          760          761          762 
## -1.037970239 -1.037970239 -1.185470444 -1.332970648  1.618387907  1.175800463 
##          763          764          765          766          767          768 
##  0.290625577  0.290625577  1.521808624  1.285734990  0.990642947  0.784078516 
##          769          770          771          772          773          774 
##  0.636532495  0.341440452 -0.101197613 -0.691381699  0.982542241  0.687409326 
##          775          776          777          778          779          780 
## -0.788255248  1.332459975  1.184867595  1.037275215  0.889682835  0.151720936 
##          781          782          783          784          785          786 
##  0.595905058  1.112462405  1.112462405  0.079982555 -0.215011687 -0.215011687 
##          787          788          789          790          791          792 
## -1.394988658 -1.394988658 -1.542485779  1.554953769 -0.510005930 -0.510005930 
##          793          794          795          796          797          798 
##  0.079982555 -0.805000173 -0.805000173 -1.394988658 -1.984977143 -1.984977143 
##          799          800          801          802          803          804 
## -0.113488085 -0.408497943  0.181521773  0.034016844 -0.260993014  1.656571065 
##          805          806          807          808          809          810 
##  0.476531632 -0.113488085 -0.408497943 -1.293527519 -1.588537377  0.737616875 
##          811          812          813          814          815          816 
##  0.678610880  0.088550929 -0.501509022 -1.091568972 -1.681628923 -1.681628923 
##          817          818          819          820          821          822 
## -2.566718849  1.707445429  1.412417104  0.232303805 -0.062724519 -0.357752844 
##          823          824          825          826          827          828 
##  1.372167615 -0.302332878 -1.040266271  0.498404178  0.439368754 -0.150985484 
##          829          830          831          832          833          834 
## -0.150985484 -0.150985484 -0.741339722  1.375950942  0.785487466 -0.543055354 
##          835          836          837          838          839          840 
## -0.838287092 -0.838287092 -0.838287092 -0.838287092  1.525642330  1.088291562 
##          841          842          843          844          845          846 
##  0.792588828  0.201183362 -1.277330304  0.201183362  1.190935451  0.895016435 
##          847          848          849          850          851          852 
##  0.747056928  0.599097420  0.510321715  0.451137912  0.303178404  0.303178404 
##          853          854          855          856          857          858 
##  0.007259389 -0.288659627 -0.584578643 -0.880497658 -1.176416674 -1.768254705 
##          859          860          861          862          863          864 
## -2.064173721  0.026495543 -0.121008321 -0.121008321 -0.416016048 -1.006031502 
##          865          866          867          868          869          870 
## -0.904625829 -0.904625829 -0.904625829 -1.789774819  0.275572825 -0.019476839 
##          871          872          873          874          875          876 
## -0.314526502 -0.314526502 -1.494725155  0.967451611  0.672331550  0.672331550 
##          877          878          879          880          881          882 
## -0.213028634 -0.213028634 -1.098388819 -1.098388819 -1.393508880 -1.983749003 
##          883          884          885          886          887          888 
##  1.418182976 -0.116239792  1.026475624  0.967451611  0.672331550  0.583795532 
##          889          890          891          892          893          894 
## -0.360588665 -1.541068911  0.478944885  0.478944885 -0.406712062 -0.406712062 
##          895          896          897          898          899          900 
## -0.849540536 -1.439978501  1.466842171 -0.600583416  0.285456121  0.285456121 
##          901          902          903          904          905          906 
## -0.600583416  1.714764548  1.714764548  1.124196732 -0.499864761  1.135779474 
##          907          908          909          910          911          912 
##  1.135779474  0.839980230  0.839980230 -0.639015990  1.902265067  0.868588778 
##          913          914          915          916          917          918 
##  0.130248572 -0.312755551 -0.903427716 -0.903427716 -1.198763799  1.606928984 
##          919          920          921          922          923          924 
##  0.868588778  0.573252696 -0.017419469 -0.312755551 -0.312755551 -1.494099881 
##          925          926          927          928          929          930 
## -0.063473354  0.686337156 -1.090420627  1.162096747 -0.315726226 -0.327791775 
##          931          932          933          934 
## -0.918423165 -1.213738860  0.669350299 -2.140295042
##MULTIPLE REGRESSION----

# Ovdje cemo raditi i druge testove 

#More predictors, x1, x2...xn
lmmodel2=lm(childHeight ~ mother+father,data=GaltonFamilies)
summary(lmmodel2)
## 
## Call:
## lm(formula = childHeight ~ mother + father, data = GaltonFamilies)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -9.117 -2.741 -0.218  2.766 11.694 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 22.64328    4.26213   5.313 1.35e-07 ***
## mother       0.29051    0.04852   5.987 3.05e-09 ***
## father       0.36828    0.04489   8.204 7.66e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.389 on 931 degrees of freedom
## Multiple R-squared:  0.1052, Adjusted R-squared:  0.1033 
## F-statistic: 54.74 on 2 and 931 DF,  p-value: < 2.2e-16
#interpretation: “ˆβ1 is the effect of x1 when all the other (specified) predictors are held constant

#fitted model is: Predicted child height=22.6433+.2905*Mother_height+.3683*Father_height.

lmmodel3=lm(childHeight ~ gender,data=GaltonFamilies)
summary (lmmodel3)
## 
## Call:
## lm(formula = childHeight ~ gender, data = GaltonFamilies)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -9.234 -1.604 -0.104  1.766  9.766 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  64.1040     0.1173  546.32   <2e-16 ***
## gendermale    5.1301     0.1635   31.38   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.497 on 932 degrees of freedom
## Multiple R-squared:  0.5137, Adjusted R-squared:  0.5132 
## F-statistic: 984.4 on 1 and 932 DF,  p-value: < 2.2e-16
#fitted model 3 is Height=64.10+5.13 ⋅Indicator for Male={64.10 if female
                                                       #69.23 if male.

#model parents and gender

#plotting the regression
library(ggplot2)
ggplot(aes(x = midparentHeight, y = childHeight, color = gender), data = GaltonFamilies) + geom_smooth(method = "lm") +
  geom_point()
## `geom_smooth()` using formula = 'y ~ x'

#graf dokazuje i koeficijente.. nakrivljenost tj slope tj. beta za musko i zensko nisu razliciti da bi bili signifikantni


lmmodel4a <-lm(childHeight ~ midparentHeight+ gender,data=GaltonFamilies)#separatly
summary (lmmodel4a) 
## 
## Call:
## lm(formula = childHeight ~ midparentHeight + gender, data = GaltonFamilies)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.5317 -1.4600  0.0979  1.4566  9.1110 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     16.51410    2.73392    6.04 2.22e-09 ***
## midparentHeight  0.68702    0.03944   17.42  < 2e-16 ***
## gendermale       5.21511    0.14216   36.69  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.17 on 931 degrees of freedom
## Multiple R-squared:  0.6332, Adjusted R-squared:  0.6324 
## F-statistic: 803.6 on 2 and 931 DF,  p-value: < 2.2e-16
#INTERACTION TERM
#The interaction effect αβij is interpreted as that part of the mean response 
#not attributable to the additive
#effect of αi and βj. For example, you may enjoy strawberries and cream individually, 
#but the combination
#is superior. In contrast, you may like fish and ice cream separatly but not together

lmmodel4 <- lm(childHeight ~ midparentHeight+ gender + midparentHeight*gender,data=GaltonFamilies) #together
summary (lmmodel4)
## 
## Call:
## lm(formula = childHeight ~ midparentHeight + gender + midparentHeight * 
##     gender, data = GaltonFamilies)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.5431 -1.4568  0.0769  1.4795  9.0860 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                18.33348    3.86636   4.742 2.45e-06 ***
## midparentHeight             0.66075    0.05580  11.842  < 2e-16 ***
## gendermale                  1.57998    5.46264   0.289    0.772    
## midparentHeight:gendermale  0.05252    0.07890   0.666    0.506    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.171 on 930 degrees of freedom
## Multiple R-squared:  0.6334, Adjusted R-squared:  0.6322 
## F-statistic: 535.6 on 3 and 930 DF,  p-value: < 2.2e-16
#childHeight = 18.5 + 0.66*midparentHeight + 1.58*gendermale + 0.053*midparentHeight*gendermale
#if there are males = 1 and increase in midparent heigh by 1cm then 0.66 + 1.58*1 +0.053*1*1

0.66+1.58+0.053 #2.293 the increase in the hight by 1cm in midpartentHeight will result to the average increse by 2.293 in sons heights (if we have significance)
## [1] 2.293
#and in case of girls 0

#childheight = 0.066*1 + 1.58*0+0.053*1*0
0.066*1 + 1.58*0+0.053*1*0 #0,06. Increase in the height by 1cma in midparetnt height will result to the average incrase by 0.066 cm in doughter higths 
## [1] 0.066
summary(lmmodel4)
## 
## Call:
## lm(formula = childHeight ~ midparentHeight + gender + midparentHeight * 
##     gender, data = GaltonFamilies)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.5431 -1.4568  0.0769  1.4795  9.0860 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                18.33348    3.86636   4.742 2.45e-06 ***
## midparentHeight             0.66075    0.05580  11.842  < 2e-16 ***
## gendermale                  1.57998    5.46264   0.289    0.772    
## midparentHeight:gendermale  0.05252    0.07890   0.666    0.506    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.171 on 930 degrees of freedom
## Multiple R-squared:  0.6334, Adjusted R-squared:  0.6322 
## F-statistic: 535.6 on 3 and 930 DF,  p-value: < 2.2e-16
#its is like fish and ice/cream



#Diagnosis of lmmodel4a

ggplot(GaltonFamilies, aes(y = midparentHeight,x= gender)) + geom_boxplot() #we have some outliers we can decide shall we extract them or not

summary (lmmodel4a) 
## 
## Call:
## lm(formula = childHeight ~ midparentHeight + gender, data = GaltonFamilies)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.5317 -1.4600  0.0979  1.4566  9.1110 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     16.51410    2.73392    6.04 2.22e-09 ***
## midparentHeight  0.68702    0.03944   17.42  < 2e-16 ***
## gendermale       5.21511    0.14216   36.69  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.17 on 931 degrees of freedom
## Multiple R-squared:  0.6332, Adjusted R-squared:  0.6324 
## F-statistic: 803.6 on 2 and 931 DF,  p-value: < 2.2e-16
#the model is y=16.514 + 0.687midparentHeight + 5.2151gendermale
#f statistics: An F statistic is a value you get when you run a regression analysis to find out if the means between two populations are significantly different. 
#It’s similar to a T statistic from a T-Test; A-T test will tell you if a single variable is statistically significant and an F test will tell you if a group of variables are jointly significant.

confint(lmmodel4a)
##                      2.5 %     97.5 %
## (Intercept)     11.1487472 21.8794572
## midparentHeight  0.6096139  0.7644165
## gendermale       4.9361179  5.4940929
#Diagnostics

plot(lmmodel4a, which=1) #they are desperzed around zero, we see the data that is "ruining" our model

#lets do correlation
vars <- c("midparentHeight", "childHeight", "mother", "father")
cor(GaltonFamilies[,vars])
##                 midparentHeight childHeight     mother     father
## midparentHeight       1.0000000   0.3209499 0.72783397 0.72843929
## childHeight           0.3209499   1.0000000 0.20132195 0.26603854
## mother                0.7278340   0.2013219 1.00000000 0.06036612
## father                0.7284393   0.2660385 0.06036612 1.00000000
#we see that there is a big correlation between midparentHeight and mother and father. This means we can not
#use in one model those variables togeter 

#Test the initial correlations
#install.packages("psych")
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
vars <- c("midparentHeight", "childHeight", "mother", "father")
corr.test(GaltonFamilies[,vars]) #It allows for all complete data to be used in the correlations
## Call:corr.test(x = GaltonFamilies[, vars])
## Correlation matrix 
##                 midparentHeight childHeight mother father
## midparentHeight            1.00        0.32   0.73   0.73
## childHeight                0.32        1.00   0.20   0.27
## mother                     0.73        0.20   1.00   0.06
## father                     0.73        0.27   0.06   1.00
## Sample Size 
## [1] 934
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##                 midparentHeight childHeight mother father
## midparentHeight               0           0   0.00   0.00
## childHeight                   0           0   0.00   0.00
## mother                        0           0   0.00   0.07
## father                        0           0   0.07   0.00
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option
#VARIANCE INFLATION FACTOR
library (SparseM) #prvo sparsM pa car
library (car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:psych':
## 
##     logit
vif(lmmodel4a) #vif je variance Inflation Factors.  koristimo da objasnimo koliko multicollinearity (correlation between predictors) postoji u regresionoj analizi.
## midparentHeight          gender 
##        1.001179        1.001179
#High VIFs are a sign of multicollinearity.vif je na neki nacin mjera nepreciznosti.
#rule of thumb:
##VIF = 1 (Not correlated)
##1 < VIF < 5 (Moderately correlated)
##VIF >=5 (Highly correlated) 

## STANDARDISED RESIDUALS FOR LMMODEL4

# Extract standardized residuals
standardized_residuals <- rstandard(lmmodel4)

# Display standardized residuals
standardized_residuals
##            1            2            3            4            5            6 
## -0.241020653  0.479301623  0.385881355  0.385881355  0.486008463  0.021700762 
##            7            8            9           10           11           12 
## -0.698271091 -0.698271091 -0.144300524  0.949215015 -0.375534380 -1.300469804 
##           13           14           15           16           17           18 
##  0.486825673 -0.669147681 -1.362731693  1.294221034 -0.089280912 -0.550448228 
##           19           20           21           22           23           24 
##  1.160049331 -0.684756638 -0.684756638  1.140198405  1.859400972  0.698419343 
##           25           26           27           28           29           30 
##  0.234026692  0.234026692  1.604448134 -1.413175105  1.848858570  0.690777115 
##           31           32           33           34           35           36 
## -0.235688048 -0.229555294 -0.301654092  1.760364680 -0.085388806  1.428941166 
##           37           38           39           40           41           42 
##  0.967521096  0.967521096  0.506101026 -0.647449150 -0.878159185  0.209247687 
##           43           44           45           46           47           48 
## -0.349282677 -2.017127657 -1.738535207 -2.201619383 -0.259940877 -0.491341294 
##           49           50           51           52           53           54 
##  0.241342228  0.469520117 -0.223612167 -0.362238623 -0.362238623 -0.824326813 
##           55           56           57           58           59           60 
##  1.412322192  0.395861907 -0.528192897 -0.990220299  1.482070942  1.020174857 
##           61           62           63           64           65           66 
##  0.327330729 -3.829734037  0.478100574 -1.461650845  0.329458579 -0.363070867 
##           67           68           69           70           71           72 
## -0.593914015 -1.059394689  0.363220405  0.270372845  0.131101504  1.405610286 
##           73           74           75           76           77           78 
##  0.941511062  0.709461451  0.477411839 -0.450786607  0.563726912  0.980107967 
##           79           80           81           82           83           84 
##  0.748595809  0.741227001 -0.183928185  0.450265293  1.649704626 -0.058771293 
##           85           86           87           88           89           90 
## -0.520521542  0.317271442  0.086417205 -0.144437032 -0.144437032  0.004048636 
##           91           92           93           94           95           96 
##  0.481742266 -0.902609671  0.296631454  0.296631454 -0.395293429  0.184872157 
##           97           98           99          100          101          102 
## -0.737692495 -0.395293429 -1.317859940 -0.737692495  1.449839592 -1.317859940 
##          103          104          105          106          107          108 
##  2.721924950  2.491283787  0.876795646 -0.737692495  1.179996291  1.041563446 
##          109          110          111          112          113          114 
##  0.810842037 -0.111803055  0.622602266  0.161383514  0.161383514  0.734459013 
##          115          116          117          118          119          120 
##  0.273230247  0.273230247  2.088344021  1.165976476 -0.217574841  1.502408223 
##          121          122          123          124          125          126 
## -0.342404666  1.165976476  0.935384590  0.935384590  0.243608931  1.733009834 
##          127          128          129          130          131          132 
##  0.836820233  2.866429042  1.013738804  0.550566245  0.087393685  0.273359077 
##          133          134          135          136          137          138 
##  0.997790563  0.905360672 -0.943237153  0.602528735 -0.302836468  0.766814604 
##          139          140          141          142          143          144 
##  0.766814604  0.535949032 -0.302836468 -0.764610905 -0.995498123  0.305083459 
##          145          146          147          148          149          150 
## -0.387513258 -1.080109974  0.158937969  0.766814604  2.467810154  0.620712406 
##          151          152          153          154          155          156 
##  0.158937969 -2.149934215  0.305083459 -0.098309617  0.709406639  0.709406639 
##          157          158          159          160          161          162 
##  0.247785893 -0.675455599  0.387337120 -0.074250570  1.089045612 -0.058198239 
##          163          164          165          166          167          168 
## -0.899660280 -1.268948484 -1.047877528 -1.047877528 -1.376304256  1.556559175 
##          169          170          171          172          173          174 
##  1.556559175  1.325913309  0.864621577  0.864621577  0.633975711  0.403329845 
##          175          176          177          178          179          180 
##  0.172683979  0.771808127  0.310282071  0.079519043  0.906701812 -0.869758129 
##          181          182          183          184          185          186 
## -0.869758129 -0.869758129  0.804648482  0.343167804 -0.118312874  0.475652387 
##          187          188          189          190          191          192 
## -0.216535507 -0.216535507 -1.370181998  1.348127528 -1.293700812  1.459782423 
##          193          194          195          196          197          198 
##  0.076136900  0.602878418 -1.242041531  0.716187519  0.254949781 -0.436906827 
##          199          200          201          202          203          204 
##  0.691641148  0.230453062  0.230453062 -2.536675456  0.337212609  0.337212609 
##          205          206          207          208          209          210 
## -0.123993418 -0.585199444 -0.585199444  0.691641148  0.691641148  0.230453062 
##          211          212          213          214          215          216 
## -0.354596431  0.869244071  0.408073002  0.271172295 -0.651246512 -1.112455915 
##          217          218          219          220          221          222 
##  1.330415140  0.638658536  0.638658536  0.271172295 -1.804270020  0.319256967 
##          223          224          225          226          227          228 
## -0.041712207 -0.041712207 -0.041712207 -0.733507081  0.638658536  0.408073002 
##          229          230          231          232          233          234 
## -0.053098067 -0.053098067 -1.436611274  0.271172295  0.040567593 -0.717433101 
##          235          236          237          238          239          240 
##  1.813579355  0.658590833 -0.210358217 -0.673011573 -1.135664929  1.351019352 
##          241          242          243          244          245          246 
## -0.673011573 -1.274460936  0.888452045 -0.036682569 -0.961817183 -1.886951797 
##          247          248          249          250          251          252 
##  1.816597346 -0.316028236 -0.777827356  0.292894591 -0.630614026 -2.477631262 
##          253          254          255          256          257          258 
##  0.985526055  1.157910445  0.696268349  0.696268349 -1.842763183  1.852297552 
##          259          260          261          262          263          264 
##  0.929082501  0.605957234  0.375153471  0.375153471 -0.455740075 -0.686543838 
##          265          266          267          268          269          270 
##  0.880178959  0.049680629 -0.503984924 -0.734678904  1.468431210  0.084307495 
##          271          272          273          274          275          276 
## -1.530503505 -1.761190791 -1.761190791  1.423586748  0.962281172  0.270322807 
##          277          278          279          280          281          282 
## -2.266857863 -2.266857863 -0.070155340 -0.762107781 -1.223409409 -1.344246710 
##          283          284          285          286          287          288 
## -2.266857863 -0.070155340 -0.992758595 -0.992758595  0.039670019  0.039670019 
##          289          290          291          292          293          294 
##  1.083098729  0.160495474  0.160495474 -0.762107781  4.191420207  2.346197901 
##          295          296          297          298          299          300 
##  0.500975595  2.005701984  1.083098729  0.483406613 -1.223409409  1.423586748 
##          301          302          303          304          305          306 
##  1.192933960  0.160495474 -0.421635557 -0.421635557  1.015490211  0.092965394 
##          307          308          309          310          311          312 
## -0.506675737  0.901753925  0.440489350  0.071477691 -0.020775224  0.540132198 
##          313          314          315          316          317          318 
##  0.217297176 -0.705088601 -1.166281489 -1.627474378 -2.088667267  1.247443884 
##          319          320          321          322          323          324 
##  0.217297176 -0.705088601 -1.304639356  0.555629906  1.104192471  0.181775879 
##          325          326          327          328          329          330 
## -0.860931581 -1.460518426 -1.967861142  1.980005836 -0.095547061 -0.326164050 
##          331          332          333          334          335          336 
## -0.787398027 -0.787398027 -0.787398027  0.479476752  0.248856266 -2.139893323 
##          337          338          339          340          341          342 
## -0.705088601  0.325025247 -0.136184072 -1.058602710  0.678490065  0.217297176 
##          343          344          345          346          347          348 
##  0.217297176  0.217297176 -0.013299268 -0.474492157  2.169862522  0.325025247 
##          349          350          351          352          353          354 
## -0.136184072  0.270578590 -1.112950781 -1.804715466 -0.225173264 -0.455772765 
##          355          356          357          358          359          360 
## -0.778612066 -0.916971767 -1.470410569  0.312678642 -0.379075070  0.643987862 
##          361          362          363          364          365          366 
## -0.278405176  1.465601494 -0.148490499 -1.070828781  0.182791343 -0.047806916 
##          367          368          369          370          371          372 
##  0.767270332 -0.846824508  1.560286380 -0.284499927 -0.527424749 -0.988594025 
##          373          374          375          376          377          378 
## -0.109984211 -0.894034580  0.394913805 -1.219178663 -0.894034580 -1.355240679 
##          379          380          381          382          383          384 
##  1.412718748  0.490332070  0.259735400  0.259735400 -0.432054608  0.578002829 
##          385          386          387          388          389          390 
##  0.116761698 -0.344479432  0.721680343  0.260510580  0.029925698 -1.814753354 
##          391          392          393          394          395          396 
##  0.032034589 -0.198568931 -0.336931042  1.412718748  1.412718748 -1.728202824 
##          397          398          399          400          401          402 
##  1.304194293 -0.633640643 -0.456169256 -1.610252404  0.984386470  0.060923088 
##          403          404          405          406          407          408 
##  0.984386470  0.984386470  1.890076466 -1.345098659  0.308423031  2.211235295 
##          409          410          411          412          413          414 
## -0.563112811  1.778690719  1.316159141  0.391095984  1.891708578  0.043679800 
##          415          416          417          418          419          420 
## -0.418327395  0.198396547  0.198396547 -0.032578752 -0.263554051 -0.263554051 
##          421          422          423          424          425          426 
## -0.725504649 -0.725504649 -0.477798765  1.410461617  0.949144146  0.810748904 
##          427          428          429          430          431          432 
##  0.718485410  0.378962610  0.148306603 -0.912711026 -1.004973428  0.580090168 
##          433          434          435          436          437          438 
##  0.487826674  0.026509202  2.209371267  1.747917181  0.825009009 -0.790080294 
##          439          440          441          442          443          444 
##  0.026509202 -0.665467005 -0.896125741  0.148306603 -0.774317422 -1.004973428 
##          445          446          447          448          449          450 
##  1.321885954  0.399064788  0.168359496  0.168359496 -1.446577545 -1.907988128 
##          451          452          453          454          455          456 
## -1.779560506  0.086101199 -1.298449004 -0.240945468 -0.471691282  0.399064788 
##          457          458          459          460          461          462 
##  1.680901447  1.219506520  0.527414129 -0.856770652 -0.856770652  0.405640298 
##          463          464          465          466          467          468 
##  0.405640298  0.174938839  0.174938839  0.764197489  0.302809470 -0.158578550 
##          469          470          471          472          473          474 
##  1.410461617  0.949144146 -0.434808269 -0.434808269  0.840274622  0.378962610 
##          475          476          477          478          479          480 
##  0.378962610  0.148306603 -0.313005409  0.204141233 -0.487656036 -1.179453306 
##          481          482          483          484          485          486 
##  0.312838042 -0.148375058 -0.378981608 -1.532014359  0.072360573 -0.711671731 
##          487          488          489          490          491          492 
## -4.401235516  0.411173857  0.088326053 -0.280642866 -1.987124116  1.132843974 
##          493          494          495          496          497          498 
## -1.265223646  0.446912344 -0.244885229  0.790321447  0.098226920  1.795267539 
##          499          500          501          502          503          504 
## -0.049862639  1.481907315  1.943134113  0.559453719  0.559453719 -2.207907071 
##          505          506          507          508          509          510 
##  1.103343722 -0.741786456  0.701180520 -0.912971368 -1.374157622  0.559705560 
##          511          512          513          514          515          516 
## -0.362757989 -0.823989763 -1.285221537  0.701180520 -0.039895747 -0.362757989 
##          517          518          519          520          521          522 
## -0.453282824  2.038840862  1.577545697  0.193660203  2.121464364  1.198714847 
##          523          524          525          526          527          528 
##  1.198714847  0.275965330 -0.115958232  1.577545697  0.654955367 -0.359893995 
##          529          530          531          532          533          534 
##  0.822849110  0.592168151  0.499895768  0.038533850  0.038533850 -0.422828067 
##          535          536          537          538          539          540 
## -0.653509026 -0.884189984  1.109656602  0.648366978  0.648366978  0.187077354 
##          541          542          543          544          545          546 
##  0.032436506 -0.428919111 -0.428919111 -0.659596919  1.660089605 -0.185409429 
##          547          548          549          550          551          552 
##  1.268394833  1.268394833  0.832985251  0.602239706  0.371494161 -0.027543691 
##          553          554          555          556          557          558 
## -0.489122109 -0.007706493 -0.007706493 -0.238512549 -1.854154937 -2.315767048 
##          559          560          561          562          563          564 
## -2.315767048  0.279571623 -0.412991890 -0.412991890  1.325060550  2.523902941 
##          565          566          567          568          569          570 
## -0.017410316  0.632151645 -0.753666164 -0.387055881 -0.941524228  0.773866107 
##          571          572          573          574          575          576 
## -0.263247653 -2.075303680  1.315325901  0.392489163  0.982726693  0.019928548 
##          577          578          579          580          581          582 
## -0.441395055 -0.441395055 -0.579792136 -0.672056857 -0.672056857 -0.902718658 
##          583          584          585          586          587          588 
## -0.902718658 -0.902718658 -1.733101143 -0.688159281 -0.784321005 -1.476071741 
##          589          590          591          592          593          594 
## -1.564909885 -0.078280751  1.063784361  0.971549475  0.602609932 -1.795497100 
##          595          596          597          598          599          600 
##  1.074714327  0.475156886 -1.231275829  1.290931203  1.290931203  1.848802031 
##          601          602          603          604          605          606 
##  0.926400118 -0.082572600 -0.635980352 -1.558326607 -1.558326607 -0.994580899 
##          607          608          609          610          611          612 
## -1.225179641 -1.455778383 -1.916975867  1.208712156  0.516952466 -0.313159163 
##          613          614          615          616          617          618 
## -0.405393789 -0.774332291 -0.072185930 -0.302784672 -0.533383414  1.007505073 
##          619          620          621          622          623          624 
## -0.376153085 -0.376153085 -0.837372472  0.629857282  0.168583665 -0.292689951 
##          625          626          627          628          629          630 
## -0.292689951 -0.753963568 -1.215237184 -1.298591858 -2.682250017  0.860494090 
##          631          632          633          634          635          636 
## -0.753963568  1.099748950  1.099748950  0.085066301 -0.145543392 -0.523326760 
##          637          638          639          640          641          642 
## -0.836169926  0.085066301 -0.837372472 -1.298591858  1.091130898  1.091130898 
##          643          644          645          646          647          648 
##  0.629857282  0.629857282  0.629857282  0.168583665 -0.292689951  0.954834974 
##          649          650          651          652          653          654 
##  0.724154606 -0.198566868 -1.121288342 -1.121288342  1.256156358 -0.589585550 
##          655          656          657          658          659          660 
## -0.820303289  2.167150185  0.552869577  0.322258061  0.866598096 -0.659927605 
##          661          662          663          664          665          666 
##  0.724154606 -0.198566868  0.564003143  0.333285404  1.185515343 -0.198566868 
##          667          668          669          670          671          672 
## -1.121288342 -1.351968711 -1.582649079 -0.128150073 -0.589585550 -0.589585550 
##          673          674          675          676          677          678 
## -0.659927605 -0.659927605 -1.121288342  0.333285404  0.102567665 -1.051021027 
##          679          680          681          682          683          684 
## -0.277331879  0.724154606  0.602257152  0.141002887 -0.781505642 -1.473387040 
##          685          686          687          688          689          690 
##  0.451074624  0.120991561 -0.274492707 -0.670199975 -0.801938247  0.387979479 
##          691          692          693          694          695          696 
##  0.157023506 -1.690624272  0.432407447  0.201393405 -0.301340503 -1.514055856 
##          697          698          699          700          701          702 
## -0.558330030 -0.558330030 -1.020789898 -1.020789898 -1.020789898 -2.870629370 
##          703          704          705          706          707          708 
##  0.104073820 -0.127087563 -0.358248945 -1.020789898  0.893687988  0.513864461 
##          709          710          711          712          713          714 
## -0.874609451 -1.013456842 -1.337434089 -1.337434089 -1.800258726  1.458164918 
##          715          716          717          718          719          720 
##  1.458164918  0.069216127 -0.393766803  1.565069448  1.565069448  1.011667384 
##          721          722          723          724          725          726 
##  0.650692158 -0.271701034  0.727809189  1.738939576  0.816605915  0.678255866 
##          727          728          729          730          731          732 
##  0.355439085 -0.105727745  1.836605162  0.683607366 -0.930589548  0.394754905 
##          733          734          735          736          737          738 
## -0.619912514  0.848287102  0.617654513  0.387021924 -0.074243254  0.444263748 
##          739          740          741          742          743          744 
## -0.016914768  0.765922846  0.765922846 -0.387129680  0.444263748 -0.616446840 
##          745          746          747          748          749          750 
## -0.387129680 -1.770792712 -1.772943672 -0.766141020 -0.766141020  0.912024665 
##          751          752          753          754          755          756 
## -0.010335870  0.080186805  0.080186805 -0.381036713 -0.381036713 -0.381036713 
##          757          758          759          760          761          762 
## -0.381036713 -0.381036713 -0.611648472 -0.842260231  1.402991488  0.710970679 
##          763          764          765          766          767          768 
## -0.673070938 -0.673070938  1.261441730  0.892283282  0.430835222  0.107821579 
##          769          770          771          772          773          774 
## -0.122902451 -0.584350511 -1.276522602  0.172729173  0.427527319 -0.034043974 
##          775          776          777          778          779          780 
##  0.024308855  0.984910087  0.754045767  0.523181447  0.292317127  1.498313985 
##          781          782          783          784          785          786 
## -0.140267906  0.572865159  0.572865159 -1.041219312 -1.502386304 -1.502386304 
##          787          788          789          790          791          792 
## -0.942776289 -0.942776289 -1.173375173  1.264615647 -1.963553295 -1.963553295 
##          793          794          795          796          797          798 
##  1.363212555 -0.020380751 -0.020380751 -0.942776289 -1.865171826 -1.865171826 
##          799          800          801          802          803          804 
## -1.324871569 -1.786077598  1.527967289  1.297338675  0.836081446  1.442364604 
##          805          806          807          808          809          810 
## -0.402459512  1.066710061  0.605452832 -0.778318853 -1.239576081  0.015786969 
##          811          812          813          814          815          816 
## -0.076465782 -0.998993294 -1.921520805 -0.459373661 -1.382025562 -1.382025562 
##          817          818          819          820          821          822 
## -2.766003415  1.531361252  1.070102303 -0.774933494  1.149156877  0.687836492 
##          823          824          825          826          827          828 
##  1.054368427  0.787191127 -0.367300408 -0.321087511 -0.413428590 -1.336839381 
##          829          830          831          832          833          834 
##  1.024219150  1.024219150  0.100601511  1.060987173  0.137243402  0.414052353 
##          835          836          837          838          839          840 
## -0.047933355 -0.047933355 -0.047933355 -0.047933355  1.323011032  0.666439318 
##          841          842          843          844          845          846 
##  0.203114681 -0.723534593 -0.716829817 -0.723534593  0.846124420  0.382126826 
##          847          848          849          850          851          852 
##  0.150128029 -0.081870767 -0.221070046 -0.313869564 -0.545868361  1.768702623 
##          853          854          855          856          857          858 
##  1.304506766  0.840310910  0.376115054 -0.088080803 -0.552276659 -1.480668372 
##          859          860          861          862          863          864 
## -1.944864228 -1.107412939 -1.338012981 -1.338012981  0.593252201 -0.329247335 
##          865          866          867          868          869          870 
## -2.544643971 -0.164721716 -0.164721716 -1.548897078 -0.699357516 -1.160679130 
##          871          872          873          874          875          876 
## -1.622000744  0.758061859 -1.087505291  0.401155389 -0.060377054 -0.060377054 
##          877          878          879          880          881          882 
##  0.923151657  0.923151657 -0.461717982 -0.461717982 -0.923341195 -1.846587622 
##          883          884          885          886          887          888 
##  1.096599623  1.071313871  0.493461878  0.401155389 -0.060377054 -0.198836787 
##          889          890          891          892          893          894 
##  0.692340050 -1.154152802 -0.344365930 -0.344365930  0.626673800  0.626673800 
##          895          896          897          898          899          900 
## -0.066242873 -0.990131771  1.220047500  0.329847182 -0.628847022 -0.628847022 
##          901          902          903          904          905          906 
##  0.329847182  1.599183059  1.599183059  0.675120052  0.484438156  0.749543652 
##          907          908          909          910          911          912 
##  0.749543652  0.285919070  0.285919070  0.286928956  1.900063507  0.282392966 
##          913          914          915          916          917          918 
##  1.473410153  0.779926473 -0.144718434 -0.144718434 -0.607040888  1.437871924 
##          919          920          921          922          923          924 
##  0.282392966  2.166893833  1.242248926  0.779926473  0.779926473 -1.069363341 
##          925          926          927          928          929          930 
## -1.158444911  0.070467932 -0.411776633  3.100518364  0.785231044 -1.592531201 
##          931          932          933          934 
## -0.169132772 -0.631389516  0.014419231 -2.068227579
# Residuals vs. Fitted values plot
plot(lmmodel4$fitted.values, standardized_residuals, 
     xlab = "Fitted Values", ylab = "Standardized Residuals",
     main = "Residuals vs Fitted")
abline(h = 0, col = "red")

#sa nasim rezultatom smo ok VIF je oko 1
##DISPLAYING REGRESSION RESULTS
#displaying regression results
library(xtable)
library(texreg)
## Version:  1.39.4
## Date:     2024-07-23
## Author:   Philip Leifeld (University of Manchester)
## 
## Consider submitting praise using the praise or praise_interactive functions.
## Please cite the JSS article in your publications -- see citation("texreg").
screenreg(list(lmmodel, lmmodel2, lmmodel3, lmmodel4, lmmodel4a))
## 
## ======================================================================================
##                             Model 1     Model 2     Model 3     Model 4     Model 5   
## --------------------------------------------------------------------------------------
## (Intercept)                  22.64 ***   22.64 ***   64.10 ***   18.33 ***   16.51 ***
##                              (4.27)      (4.26)      (0.12)      (3.87)      (2.73)   
## midparentHeight               0.64 ***                            0.66 ***    0.69 ***
##                              (0.06)                              (0.06)      (0.04)   
## mother                                    0.29 ***                                    
##                                          (0.05)                                       
## father                                    0.37 ***                                    
##                                          (0.04)                                       
## gendermale                                            5.13 ***    1.58        5.22 ***
##                                                      (0.16)      (5.46)      (0.14)   
## midparentHeight:gendermale                                        0.05                
##                                                                  (0.08)               
## --------------------------------------------------------------------------------------
## R^2                           0.10        0.11        0.51        0.63        0.63    
## Adj. R^2                      0.10        0.10        0.51        0.63        0.63    
## Num. obs.                   934         934         934         934         934       
## ======================================================================================
## *** p < 0.001; ** p < 0.01; * p < 0.05
#displaying for descriptive statistics- also stargazer good and regreesion
library(stargazer)
## 
## Please cite as: 
## 
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
stargazer(GaltonFamilies, type = "text", title = "Descriptive statistics", digits = 2, out = "Descriptive statistics.txt")
## 
## Descriptive statistics
## ==============================================
## Statistic        N  Mean  St. Dev.  Min   Max 
## ----------------------------------------------
## father          934 69.20   2.48   62.00 78.50
## mother          934 64.09   2.29   58.00 70.50
## midparentHeight 934 69.21   1.80   64.40 75.43
## children        934 6.17    2.73     1    15  
## childNum        934 3.59    2.36     1    15  
## childHeight     934 66.75   3.58   56.00 79.00
## ----------------------------------------------
#the dabase HAS TO BE in dataframe format. Then, it will give the descriptive statistics for all numeric variables

stargazer(lmmodel, lmmodel2, lmmodel3, lmmodel4, lmmodel4a, type = "text", title="Results", align=TRUE, out = "regressions.txt")
## 
## Results
## ======================================================================================================================================================
##                                                                                Dependent variable:                                                    
##                            ---------------------------------------------------------------------------------------------------------------------------
##                                                                                    childHeight                                                        
##                                      (1)                      (2)                     (3)                      (4)                      (5)           
## ------------------------------------------------------------------------------------------------------------------------------------------------------
## midparentHeight                    0.637***                                                                  0.661***                 0.687***        
##                                    (0.062)                                                                   (0.056)                  (0.039)         
##                                                                                                                                                       
## mother                                                     0.291***                                                                                   
##                                                             (0.049)                                                                                   
##                                                                                                                                                       
## father                                                     0.368***                                                                                   
##                                                             (0.045)                                                                                   
##                                                                                                                                                       
## gendermale                                                                          5.130***                  1.580                   5.215***        
##                                                                                     (0.164)                  (5.463)                  (0.142)         
##                                                                                                                                                       
## midparentHeight:gendermale                                                                                    0.053                                   
##                                                                                                              (0.079)                                  
##                                                                                                                                                       
## Constant                          22.636***                22.643***               64.104***                18.333***                16.514***        
##                                    (4.265)                  (4.262)                 (0.117)                  (3.866)                  (2.734)         
##                                                                                                                                                       
## ------------------------------------------------------------------------------------------------------------------------------------------------------
## Observations                         934                      934                     934                      934                      934           
## R2                                  0.103                    0.105                   0.514                    0.633                    0.633          
## Adjusted R2                         0.102                    0.103                   0.513                    0.632                    0.632          
## Residual Std. Error            3.392 (df = 932)        3.389 (df = 931)         2.497 (df = 932)         2.171 (df = 930)         2.170 (df = 931)    
## F Statistic                107.029*** (df = 1; 932) 54.742*** (df = 2; 931) 984.402*** (df = 1; 932) 535.585*** (df = 3; 930) 803.636*** (df = 2; 931)
## ======================================================================================================================================================
## Note:                                                                                                                      *p<0.1; **p<0.05; ***p<0.01
#otvorite folder u kojem se nalayite i otvorite file regression