Skip to contents

Regress vectors

Correlate x and y: %/r%

Polynomial regression of degree 1: %/1%

Polynomial regression of degree 2: %/2%

Polynomial regression of degree 3: %/3%

Polynomial regression of degree 4: %/4%

%/n% Polynomial regression of degree n: %/n%

Usage

x %/r% y

x %/1% y

x %/2% y

x %/3% y

x %/4% y

x %/n% yn

Arguments

x

Numeric vectors

y

Numeric vector

yn

List of length 2, first element is a vector y, the second element an integer denoting the order of the polynomial regression.

Examples


x <- rnorm(100)
y <- x + x^2 + x^3

# Correlate x with y
x%/r%y
#> [1] 0.8851748

# Polynomial regression degree 1 .. 4
x%/1%y
#> 
#> Call:
#> stats::lm(formula = y ~ stats::poly(x, order = 1))
#> 
#> Coefficients:
#>               (Intercept)  stats::poly(x, order = 1)  
#>                     1.802                     37.633  
#> 
x%/2%y
#> 
#> Call:
#> stats::lm(formula = y ~ stats::poly(x, order = 2))
#> 
#> Coefficients:
#>                (Intercept)  stats::poly(x, order = 2)1  
#>                      1.802                      37.633  
#> stats::poly(x, order = 2)2  
#>                     15.704  
#> 
x%/3%y
#> 
#> Call:
#> stats::lm(formula = y ~ stats::poly(x, order = 3))
#> 
#> Coefficients:
#>                (Intercept)  stats::poly(x, order = 3)1  
#>                      1.802                      37.633  
#> stats::poly(x, order = 3)2  stats::poly(x, order = 3)3  
#>                     15.704                      12.027  
#> 
x%/4%y
#> 
#> Call:
#> stats::lm(formula = y ~ stats::poly(x, order = 4))
#> 
#> Coefficients:
#>                (Intercept)  stats::poly(x, order = 4)1  
#>                  1.802e+00                   3.763e+01  
#> stats::poly(x, order = 4)2  stats::poly(x, order = 4)3  
#>                  1.570e+01                   1.203e+01  
#> stats::poly(x, order = 4)4  
#>                  2.117e-15  
#> 

anova(x%/1%y,x%/2%y,x%/3%y,x%/4%y)
#> Analysis of Variance Table
#> 
#> Model 1: y ~ stats::poly(x, order = 1)
#> Model 2: y ~ stats::poly(x, order = 2)
#> Model 3: y ~ stats::poly(x, order = 3)
#> Model 4: y ~ stats::poly(x, order = 4)
#>   Res.Df    RSS Df Sum of Sq          F Pr(>F)    
#> 1     98 391.26                                   
#> 2     97 144.64  1    246.61 1.2494e+32 <2e-16 ***
#> 3     96   0.00  1    144.65 7.3280e+31 <2e-16 ***
#> 4     95   0.00  1      0.00 2.2706e+00 0.1352    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

# Order n

x%/n%list(y,10)
#> 
#> Call:
#> stats::lm(formula = yn[[1]] ~ stats::poly(x, order = yn[[2]]))
#> 
#> Coefficients:
#>                       (Intercept)   stats::poly(x, order = yn[[2]])1  
#>                         1.802e+00                          3.763e+01  
#>  stats::poly(x, order = yn[[2]])2   stats::poly(x, order = yn[[2]])3  
#>                         1.570e+01                          1.203e+01  
#>  stats::poly(x, order = yn[[2]])4   stats::poly(x, order = yn[[2]])5  
#>                         2.117e-15                         -8.893e-16  
#>  stats::poly(x, order = yn[[2]])6   stats::poly(x, order = yn[[2]])7  
#>                        -1.258e-15                         -5.920e-16  
#>  stats::poly(x, order = yn[[2]])8   stats::poly(x, order = yn[[2]])9  
#>                        -2.820e-15                          1.996e-15  
#> stats::poly(x, order = yn[[2]])10  
#>                         7.173e-17  
#>