knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(lm.hua) library(broom) library(bench)
Define a predictor and target vector
Y = matrix(c(1714,1664,1760,1685,1693,1670,1764,1764,1792,1850,1735,1775), ncol=1) X = matrix(c(2.4,2.52,2.54,2.74,2.83,2.91,3,3,3.01,3.01,3.02,3.07), ncol=1)
To use this function 'linear_regression' to get Y = beta0 + beta1*X + e:
linear_regression(X, Y)
The built-in linear modeling function lm:
lm(Y ~ X)
Comparing beta0 and beta1
beta0_hua = linear_regression(X, Y)[1,1] beta1_hua = linear_regression(X, Y)[2,1] beta0_org = tidy(lm(Y ~ X))$estimate[1] beta1_org = tidy(lm(Y ~ X))$estimate[2] all.equal(beta0_hua, beta0_org) all.equal(beta0_hua, beta0_org) #beta0_org = loc(lm(Y ~ X))
Comparing performance of the functions
bench::mark(linear_regression(X, Y)[1,1], tidy(lm(Y ~ X))$estimate[1], check = function(x,y){all.equal(beta0_hua, beta0_org, tolerance = 1e-2)}) #bench::mark(linear_regression(X, Y), linear_regression(X, Y), check = function(x,y){all.equal(beta0_hua, beta0_org, tolerance = 1e-2)}) #beta0_org = loc(lm(Y ~ X))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.