-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
201 lines (141 loc) · 7.32 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# linr
<!-- badges: start -->
[![R-CMD-check](https://github.com/SelinaSong0412/linr/workflows/R-CMD-check/badge.svg)](https://github.com/SelinaSong0412/linr/actions)
[![codecov](https://codecov.io/gh/SelinaSong0412/linr/branch/main/graph/badge.svg?token=K6NRF4WUNZ)](https://codecov.io/gh/SelinaSong0412/linr)
<!-- badges: end -->
`linr` is used to fit a linear model. In this function, linear regression can be done by three matrix decomposition methods, which are the QR decomposition, Cholesky decomposition and the singular value decomposition (SVD). The defalt fitting method used is the Cholesky decomposition method. All three decomposition methods can fit linear model with high efficiency.
## Installation
You can install the development version of linr from [GitHub](https://github.com/) with:
```{r}
# install.packages("devtools")
devtools::install_github("SelinaSong0412/linr")
```
## Usage
To learn the whole usage of `linr`, you can see `vignette("Intro_to_linr", package = "linr")` for an overview of the package. Also see `vignette("Efficiency_tests", package = "linr"` for efficiency testing of linr, which compares output with the `stats::lm` function.
## Example
The following are basic examples which shows you how to fit a linear model with `linr` You can look at the examples and practice with the whole usage of `linr`.
First, library 'linr' function.
```{r setup}
library(linr)
```
### Using linr with defined vector Y (observations) and vector/matrix X (predictions). (Without dataset)
**Conducting simple linear regression with `linr`**
You can fit your model like this:
```{r}
y = rnorm(300)
x = rnorm(300)
model.linr <- linr(y ~ x)
```
Then you could check many items generated by `linr` as follows:
```{r}
model.linr$Call # This is how your model looks like
data.frame(model.linr$coefficients, # Coefficient estimators
model.linr$std.error, # Standard error of estimators
model.linr$T_statistic, # T statistics of estimators
model.linr$p_value.T) # p value for T test
# Other items also can be checked by
data.frame(model.linr$MSE, # Mean square error
model.linr$R.square, # R^2
model.linr$Adj.R.square, # Adjusted R^2
model.linr$F_statistic, # F statistics of estimators
model.linr$p_value.F) # p value for F test
# You could also look at the fitted values and residuals like this:
# head(model.linr$fitted.values) # Look at the first 6 fitted values
# head(model.linr$residuals) # Look at the first 6 residuals
```
**Conducting multiple linear regression with `linr`**
You can fit your multiple regression model with 3 options of matrix decompositiom methods:
(1). [QR decompositiom methods](https://en.wikipedia.org/wiki/QR_decomposition)
(2). [SVD decompositiom methods](https://en.wikipedia.org/wiki/Singular_value_decomposition)
(3). [Cholesky decompositiom methods](https://en.wikipedia.org/wiki/Cholesky_decomposition) (Default)
Here we first look at the default method:
```{r}
Y = rnorm(300)
X = matrix(rnorm(6000), nrow = 300, ncol = 20)
model.linr.mul <- linr(Y ~ X, method = "cholesky")
# model.linr.mul <- linr(Y ~ X) # This do the same thing
```
You can use the other two method like this.
```{r}
# model.linr.mul <- linr(Y ~ X, method = "qr") # Using QR decomposition
# model.linr.mul <- linr(Y ~ X, method = "svd") # Using SVD decomposition
```
Then you could check many items generated by `linr` as follows:
```{r}
model.linr.mul$Call # This is how your model looks like
data.frame(model.linr.mul$coefficients, # Coefficient estimators
model.linr.mul$std.error, # Standard error of estimators
model.linr.mul$T_statistic, # T statistics of estimators
model.linr.mul$p_value.T) # p value for T test
# Other items also can be checked by
head(data.frame(model.linr.mul$MSE, # Mean square error
model.linr.mul$R.square, # R^2
model.linr.mul$Adj.R.square, # Adjusted R^2
model.linr.mul$F_statistic, # F statistics of estimators
model.linr.mul$p_value.F)) # p value for F test
# You could also look at the fitted values and residuals like this:
# head(model.linr.mul$fitted.values) # Look at the first 6 fitted values
# head(model.linr.mul$residuals) # Look at the first 6 residuals
```
### Using linr with regression formula and your own dataset
**Conducting simple linear regression with `linr` on your data**
```{r}
# Here we use the R build-in dataset cars as an example:
# fit a linear model with dist as outcome and speed as predictor
model.linr.cars <- linr(dist ~ speed, data = cars)
```
Then you could check items generated by `linr` as follows:
```{r}
model.linr.cars$Call # This is how your model looks like
data.frame(model.linr.cars$coefficients, # Coefficient estimators
model.linr.cars$std.error, # Standard error of estimators
model.linr.cars$T_statistic, # T statistics of estimators
model.linr.cars$p_value.T) # p value for T test
# Other items also can be checked by
data.frame(model.linr.cars$MSE, # Mean square error
model.linr.cars$R.square, # R^2
model.linr.cars$Adj.R.square, # Adjusted R^2
model.linr.cars$F_statistic, # F statistics of estimators
model.linr.cars$p_value.F) # p value for F test
# You could also look at the fitted values and residuals like this:
# head(model.linr.cars$fitted.values) # Look at the first 6 fitted values
# head(model.linr.cars$residuals) # Look at the first 6 residuals
```
**Conducting multiple linear regression with `linr` on your data**
```{r}
# Here we use the R build-in dataset mtcars as an example:
# fit a linear model with disp as outcome and mpg, wt, carb as predictors
model.linr.mtcars <- linr(disp ~ mpg + wt + carb, data = mtcars)
```
Then you could check items generated by `linr` as follows:
```{r}
model.linr.mtcars$Call # This is how your model looks like
data.frame(model.linr.mtcars$coefficients, # Coefficient estimators
model.linr.mtcars$std.error, # Standard error of estimators
model.linr.mtcars$T_statistic, # T statistics of estimators
model.linr.mtcars$p_value.T) # p value for T test
# Other items also can be checked by
data.frame(model.linr.mtcars$MSE, # Mean square error
model.linr.mtcars$R.square, # R^2
model.linr.mtcars$Adj.R.square, # Adjusted R^2
model.linr.mtcars$F_statistic, # F statistics of estimators
model.linr.mtcars$p_value.F) # p value for F test
# You could also look at the fitted values and residuals like this:
# head(model.linr.mtcars$fitted.values) # Look at the first 6 fitted values
# head(model.linr.mtcars$residuals) # Look at the first 6 residuals
```
## Useful links:
* [Github page](https://github.com/SelinaSong0412/linr)
* [Report bug](https://github.com/SelinaSong0412/linr/issues)