lab4

Udaya Shanker Mohanan Nair, Pranav Pankaj Chandode

library(lab4)

Introduction

This vignette illustrates how to use lab4 package to perform linear progression models and to perform various special functions like print(), plot(), resid(), pred(),coef() and summary().

DataSet

For testing purpose, here we are using iris dataset, containing measurements of features of flower iris. To load data use the following:

data(iris)

Invoking linreg function

This Function will take two parameters namely formula and data. Type of formula is an expression specifying the relationship between dependent and independent variables, whereas data is the dataset(iris in this case).

  linreg_mod <- linreg$new(Petal.Length~Sepal.Width+Sepal.Length, data=iris)
#> [1] "Petal.Length ~ Sepal.Width + Sepal.Length"

Plot Function

Plotting for residuals against Fitted Values and for Scale-Locations against sqaure root of standardized residuals.

plots <- linreg_mod$plot()
#> Warning in sqrt(residuals/(sqrt(abs(res_variance)))): NaNs produced
print(plots[[1]])

print(plots[[2]])
#> Warning: Removed 79 rows containing non-finite outside the scale range
#> (`stat_summary()`).
#> Warning: Removed 79 rows containing missing values or values outside the scale range
#> (`geom_point()`).

## Resid Function Resid Function is to display the residuals of the model as a vector.

# Get residuals
residuals <- linreg_mod$resid()
print(residuals)
#>            1            2            3            4            5            6 
#> -0.445578965 -0.759772100 -0.236928933  0.006767993 -0.134157381 -0.142807413 
#>            7            8            9           10           11           12 
#>  0.308354980 -0.301882039 -0.005838155 -0.525909771 -0.610532071  0.153236470 
#>           13           14           15           16           17           18 
#> -0.582212845  0.005583428 -1.219182103 -0.206173533 -0.542807413 -0.445578965 
#>           19           20           21           22           23           24 
#> -0.809347506  0.056008022 -0.812119057 -0.077854307  0.176079637 -0.413303622 
#>           25           26           27           28           29           30 
#>  0.453236470 -0.737331354 -0.201882039 -0.523138219 -0.757000548  0.063071067 
#>           31           32           33           34           35           36 
#> -0.248350516 -1.012119057  0.280035754 -0.218779681 -0.525909771 -0.869606697 
#>           37           38           39           40           41           42 
#> -1.255815983  0.043401874  0.028024174 -0.479441294 -0.368019710 -1.086571383 
#>           43           44           45           46           47           48 
#>  0.295748831 -0.068019710  0.456008022 -0.582212845  0.156008022  0.040630322 
#>           49           50           51           52           53           54 
#> -0.432972816 -0.535744368 -0.920791790 -0.055436262 -0.677094864 -0.162163930 
#>           55           56           57           58           59           60 
#> -0.668444832  0.652029205  0.455985322  0.337053927 -0.712141758  0.805963150 
#>           61           62           63           64           65           66 
#> -0.175954643  0.264635354 -1.183822532  0.275654516  0.063450789 -0.821976354 
#>           67           68           69           70           71           72 
#>  1.097313118 -0.059392378 -1.038941041 -0.171998527  1.132360012 -0.558207813 
#>           73           74           75           76           77           78 
#> -0.414913309  0.141792187 -0.657023248 -0.778279429 -1.001122596 -0.355838683 
#>           79           80           81           82           83           84 
#>  0.253213770 -0.615695452 -0.228301601 -0.328301601 -0.259392378  0.585489113 
#>           85           86           87           88           89           90 
#>  1.452431627  0.922525415 -0.521976354 -1.182637967  0.697313118  0.105560728 
#>           91           92           93           94           95           96 
#>  0.639423057  0.309516845 -0.293254707  0.025632344  0.395726131  0.619753863 
#>           97           98           99          100          101          102 
#>  0.485891534 -0.301904739 -0.184202253  0.252029205  1.755985322  0.940607622 
#>          103          104          105          106          107          108 
#> -0.166075702  0.820536006  0.799279826 -0.353871975  1.670916256 -0.255056540 
#>          109          110          111          112          113          114 
#> -0.225150328  0.659539017  0.367004484  0.075252094 -0.033397938  0.750442219 
#>          115          116          117          118          119          120 
#>  1.074469951  0.744563738  0.499279826  0.639467401 -0.766880545 -0.183822532 
#>          121          122          123          124          125          126 
#>  0.256767465  1.229588460 -0.699155888 -0.147188651  0.745748303  0.024089701 
#>          127          128          129          130          131          132 
#>  0.064232932  0.609516845  0.509114423 -0.443634957 -0.766478124 -0.015651108 
#>          133          134          135          136          137          138 
#>  0.509114423  0.186673677  0.774067529 -1.031431230  1.489847651  0.810701409 
#>          139          140          141          142          143          144 
#>  0.687076099 -0.177094864  0.378023646 -0.477094864  0.940607622  0.634326720 
#>          145          146          147          148          149          150 
#>  0.745748303 -0.155838683 -0.314913309  0.199279826  1.467406905  1.164635354

pred Function

pred Function is to display the predicted values of the model.

# Get predicted values
predictions <- linreg_mod$pred()
print(predictions)
#>   [1] 1.8455790 2.1597721 1.5369289 1.4932320 1.5341574 1.8428074 1.0916450
#>   [8] 1.8018820 1.4058382 2.0259098 2.1105321 1.4467635 1.9822128 1.0944166
#>  [15] 2.4191821 1.7061735 1.8428074 1.8455790 2.5093475 1.4439920 2.5121191
#>  [22] 1.5778543 0.8239204 2.1133036 1.4467635 2.3373314 1.8018820 2.0231382
#>  [29] 2.1570005 1.5369289 1.8483505 2.5121191 1.2199642 1.6187797 2.0259098
#>  [36] 2.0696067 2.5558160 1.3565981 1.2719758 1.9794413 1.6680197 2.3865714
#>  [43] 1.0042512 1.6680197 1.4439920 1.9822128 1.4439920 1.3593697 1.9329728
#>  [50] 1.9357444 5.6207918 4.5554363 5.5770949 4.1621639 5.2684448 3.8479708
#>  [57] 4.2440147 2.9629461 5.3121418 3.0940369 3.6759546 3.9353646 5.1838225
#>  [64] 4.4243455 3.5365492 5.2219764 3.4026869 4.1593924 5.5389410 4.0719985
#>  [71] 3.6676400 4.5582078 5.3149133 4.5582078 4.9570232 5.1782794 5.8011226
#>  [78] 5.3558387 4.2467862 4.1156955 4.0283016 4.0283016 4.1593924 4.5145109
#>  [85] 3.0475684 3.5774746 5.2219764 5.5826380 3.4026869 3.8944393 3.7605769
#>  [92] 4.2904832 4.2932547 3.2743677 3.8042739 3.5802461 3.7141085 4.6019047
#>  [99] 3.1842023 3.8479708 4.2440147 4.1593924 6.0660757 4.7794640 5.0007202
#> [106] 6.9538720 2.8290837 6.5550565 6.0251503 5.4404610 4.7329955 5.2247479
#> [113] 5.5333979 4.2495578 4.0255300 4.5554363 5.0007202 6.0605326 7.6668805
#> [120] 5.1838225 5.4432325 3.6704115 7.3991559 5.0471887 4.9542517 5.9759103
#> [127] 4.7357671 4.2904832 5.0908856 6.2436350 6.8664781 6.4156511 5.0908856
#> [134] 4.9133263 4.8259325 7.1314312 4.1101523 4.6892986 4.1129239 5.5770949
#> [141] 5.2219764 5.5770949 4.1593924 5.2656733 4.9542517 5.3558387 5.3149133
#> [148] 5.0007202 3.9325931 3.9353646

Summary Function

Summary function is to display coefficients along with standard error, t-values, p-values with significance.

linreg_mod$summary()
#> Coefficients:
#>              Estimate_C Std.Error t value Pr(>|t|)    
#> (Intercept)      -2.525     0.563  -4.481        0 ***
#> Sepal.Width      -1.339     0.122 -10.940        0 ***
#> Sepal.Length      1.776     0.064  27.569        0 ***
#> 
#> Residual standard error: 0.6464805 on 147 degrees of freedom

Conclusion

This Linreg function helps to find Linear regression of models to find out Regression coefficients, fitted values, residuals, degree of freedom, residual variance, variance of regression coefficients and different ways to print, plot the model coefficients.

Instructions to Include the Vignette in Your Package

  1. Save the Vignette: Save the above content in a file named using_linreg.Rmd within the vignettes/ directory of your package.

  2. Build the Vignette: Ensure that you have the necessary packages installed for building vignettes, such as knitr and rmarkdown. Then, build your package using:

    ```r devtools::build_vignettes()