A modified `poly()` function that allows for missing values.
Poly(x, degree = 1, coefs = NULL, raw = FALSE, ...)
variable to convert to matrix
degree of polynomial
pass to poly() function
pass to poly() function
more arguments for the poly() function
a matrix, with NAs in the missing rows
Credit goes to whoever posted this online first (google search if you must find it!)
Poly(rnorm(100), degree = 2L)
#> [,1] [,2]
#> [1,] 0.155954885 0.1267817630
#> [2,] 0.201353786 0.2679711501
#> [3,] 0.168227490 0.1614315822
#> [4,] -0.044215260 -0.0704208731
#> [5,] 0.029084964 -0.0786725272
#> [6,] -0.141409368 0.0839024004
#> [7,] -0.018759924 -0.0838244750
#> [8,] -0.053849793 -0.0624225674
#> [9,] -0.007435237 -0.0861835510
#> [10,] -0.104766149 0.0065227734
#> [11,] -0.199733860 0.2549924488
#> [12,] -0.094876692 -0.0103790758
#> [13,] -0.039173830 -0.0739659365
#> [14,] 0.083097617 -0.0252535951
#> [15,] 0.048164373 -0.0655698181
#> [16,] -0.066429129 -0.0495617408
#> [17,] -0.013876529 -0.0851139173
#> [18,] -0.129624754 0.0564820301
#> [19,] 0.101735970 0.0048962582
#> [20,] -0.064824620 -0.0513545190
#> [21,] -0.007460571 -0.0861807504
#> [22,] 0.139551649 0.0845396959
#> [23,] 0.046072558 -0.0673138312
#> [24,] -0.086451934 -0.0234426149
#> [25,] -0.137876141 0.0754290524
#> [26,] -0.012153318 -0.0854704191
#> [27,] -0.167298518 0.1525803194
#> [28,] -0.022273972 -0.0826412755
#> [29,] 0.104201531 0.0093348276
#> [30,] 0.008696221 -0.0857116574
#> [31,] 0.178646116 0.1928925362
#> [32,] 0.012552378 -0.0849319546
#> [33,] -0.103522979 0.0043050947
#> [34,] -0.020033425 -0.0834203686
#> [35,] -0.031568197 -0.0784818324
#> [36,] -0.061965518 -0.0544386780
#> [37,] 0.084241153 -0.0235768857
#> [38,] 0.018242194 -0.0833115300
#> [39,] -0.029646821 -0.0794642778
#> [40,] -0.028256236 -0.0801354669
#> [41,] 0.111376527 0.0228499534
#> [42,] 0.008689104 -0.0857128584
#> [43,] 0.190256175 0.2301641733
#> [44,] 0.119198827 0.0385994290
#> [45,] 0.102417513 0.0061126691
#> [46,] 0.129311295 0.0605292149
#> [47,] 0.147892108 0.1054364146
#> [48,] 0.043251977 -0.0695455520
#> [49,] 0.183090977 0.2068862517
#> [50,] -0.012844063 -0.0853336871
#> [51,] 0.166400520 0.1561082866
#> [52,] -0.004379406 -0.0864398879
#> [53,] 0.050206940 -0.0637937953
#> [54,] 0.074113805 -0.0376388761
#> [55,] 0.111803098 0.0236815175
#> [56,] 0.014781324 -0.0843639109
#> [57,] -0.101524578 0.0007962168
#> [58,] 0.094080738 -0.0082145239
#> [59,] -0.117664919 0.0311111033
#> [60,] 0.019434268 -0.0829010512
#> [61,] -0.213204922 0.3028771109
#> [62,] -0.103817716 0.0048284532
#> [63,] 0.002659599 -0.0864155220
#> [64,] 0.008930819 -0.0856715736
#> [65,] -0.152062197 0.1107574956
#> [66,] -0.072341129 -0.0425715434
#> [67,] 0.054580444 -0.0597482153
#> [68,] -0.041042610 -0.0727031372
#> [69,] 0.126121803 0.0534214634
#> [70,] -0.125533652 0.0475248522
#> [71,] -0.100205197 -0.0014825311
#> [72,] -0.133284655 0.0647405760
#> [73,] -0.205375695 0.2746649012
#> [74,] -0.009016074 -0.0859875217
#> [75,] 0.061170620 -0.0530269985
#> [76,] -0.108917155 0.0141214515
#> [77,] 0.024859969 -0.0807220876
#> [78,] 0.046472721 -0.0669860599
#> [79,] -0.001933979 -0.0865286193
#> [80,] -0.117193396 0.0301615589
#> [81,] -0.050779377 -0.0651459247
#> [82,] -0.138215136 0.0762326583
#> [83,] -0.110246528 0.0166179925
#> [84,] -0.204889940 0.2729494558
#> [85,] 0.053922037 -0.0603784196
#> [86,] -0.076828068 -0.0368625778
#> [87,] 0.020843730 -0.0823839905
#> [88,] 0.070268203 -0.0425136191
#> [89,] 0.071565370 -0.0408979134
#> [90,] 0.100559356 0.0028151657
#> [91,] 0.014399983 -0.0844671915
#> [92,] -0.104699842 0.0064038132
#> [93,] -0.031952843 -0.0782774787
#> [94,] 0.175032181 0.1817668157
#> [95,] 0.130209084 0.0625616783
#> [96,] 0.002788076 -0.0864071095
#> [97,] 0.102035974 0.0054307115
#> [98,] -0.007392259 -0.0861882765
#> [99,] 0.033248561 -0.0763505465
#> [100,] -0.012968499 -0.0853081773
#> attr(,"degree")
#> [1] 1 2
#> attr(,"coefs")
#> attr(,"coefs")$alpha
#> [1] 0.1453126 0.1237669
#>
#> attr(,"coefs")$norm2
#> [1] 1.0000 100.0000 103.3001 142.5322
#>
#> attr(,"class")
#> [1] "poly" "matrix"
# handles missing values:
iris.na <- iris
iris.na$Sepal.Length[c(5, 10)] <- NA
lm(Sepal.Width ~ Poly(Sepal.Length, 2L), data = iris.na)
#>
#> Call:
#> lm(formula = Sepal.Width ~ Poly(Sepal.Length, 2L), data = iris.na)
#>
#> Coefficients:
#> (Intercept) Poly(Sepal.Length, 2)1 Poly(Sepal.Length, 2)2
#> 3.0534 -0.5802 0.8131
#>
# stats::poly() produces an error in this case:
# lm(Sepal.Width ~ poly(Sepal.Length, 2L), data = iris.na)