I want to estimate nonparametric estimates for an independent variable by using lokern package at R.
I have the below data which have 2 variables:
x: independent variable
y: dependent variable
data_test<-structure(list(y = c(0.875261480116371, 0.13319865469368, 0.00127171595390059,
0.120784784396633, 0.396145484602405, 0.0145083415906443, 0.126972404606687,
0.158633863426125, 0.307832433458906, 3.42112531824949, 0.0340520045576305,
0.0577209021937775, 0.0296622168402153, 0.519694575795073, 0.598147976985479,
0.0130977584533862, 0.158721871619698, 0.0345042704092106, 0.828465740122911,
0.288698762072103, 0.00612936654816416, 0.660590833311049, 0.744003852280185,
0.0218624098840227, 0.0643524619734905, 0.106603465426499, 0.996560657600869,
0.122380988335843, 0.533105113544206, 0.0275922420213573, 0.107316738492487,
0.560308948944375, 0.143056108849691, 0.0161973817777236, 0.131817444834554,
1.0931449919045, 0.676905541419034, 0.0264930182311347, 0.0219841874288544,
0.488998767576592, 0.0347315975270806, 0.0411243463454958, 0.239393381406891,
0.518050404977722, 0.0446311348629937, 1.06966798067092, 0.181537821115414,
0.0313727396250001, 0.0216034647860423, 0.0276009215712176, 0.363985328271051,
0.360296428331267, 0.0668551459074575, 0.0096603143441187, 0.0105876830440506,
0.417186148227153, 0.553820116962763, 0.0411408340216935, 0.171913862343572,
0.0514348158606772, 0.389026902873765, 0.185058082651845, 0.0369879163899647,
1.08104328029981, 0.00746768318488792, 0.605457684587019, 0.0144968484938537,
0.00602213252556212, 0.0866902221106011, 2.86731502732902, 0.0410085953665955,
0.013626354236944, 0.000188405910625031, 0.0585839444657251,
0.238292013239626, 0.0792612023923057, 0.108649755268567, 0.00461453622722967,
0.171731805762336, 0.375266208662383, 0.15081802592681, 0.0993654895544528,
1.04912058404724, 0.00688994906597782, 0.00131255129276553, 0.0299156532828988,
1.65913122506183, 1.2779796600082, 0.0635807935616347, 0.0350137919082677,
2.88472754915262, 0.451804325441365, 0.170435290928851, 0.153848212504272,
0.190051111767901, 0.0250402432610551, 1.27823234596823, 0.325244428486599,
2.55249481518541, 0.652738453178641), x = c(0.157051206555181,
93.5554103254521, 36.4963908754934, -3.56611266493445, 34.7541054260692,
-62.9400893391807, 12.0450577377795, -35.6331874250237, -39.828866846312,
55.4826489507221, -184.96284270765, 18.45318524202, 24.0251747535325,
-17.2227224445543, 72.0898450404128, 77.3400269579394, 11.4445438761823,
-39.8399136067962, 18.5753251409526, -91.0200933927729, -53.7306953307048,
7.82902710952271, -81.2767391884694, 86.2556579176221, -14.7859426091212,
25.3677870484381, -32.6501861291018, 99.8278847617673, 34.9829942023039,
73.0140475212959, -16.6109126845448, -32.7592335826843, 74.8537874088129,
-37.8227588694546, 12.7268934849489, 36.3066722290206, -104.553574396311,
82.2742694540057, -16.276676021576, -14.8270655993876, -69.9284468279249,
-18.6364153009855, 20.2791386270462, -48.9278429329243, 71.9757184735048,
21.1260821883741, 103.424754322692, 42.6072553816148, 17.7123515166677,
14.698117153582, -16.6135250838639, -60.331196596044, 60.0246972779761,
-25.8563620618713, 9.82868981305174, -10.2896467597535, -64.5899487712409,
74.419091432425, -20.2832034012612, -41.4624965895172, 22.6792451066338,
62.3720212013179, 43.0183777764626, -19.2322428203173, -103.973231184753,
8.64157577348479, -77.8111614478938, 12.0402859159796, 7.7602400256449,
-29.4432033091852, 169.331480455615, 20.2505790945828, 11.6731976068873,
-1.37261032571168, -24.2041204066012, -48.8151629352629, 28.1533661206446,
32.9620623245219, 6.79303777939566, -41.4405364060766, 61.2589755596993,
-38.8352965646988, 31.5222920414193, 102.426587566278, -8.30057170680298,
3.62291497659761, -17.2961421371642, -128.80726784859, -113.047762472691,
25.2152322142063, -18.7119726133478, -169.84485712416, -67.2163912629475,
41.2838092875223, -39.2234894552068, -43.5948519630358, 15.8241092201283,
113.058937991131, 57.0302050221283, -159.765290823302)), class = "data.frame", row.names = c(NA,
-100L))
I using lokern package at R to obtain nonparametric estimates for y.
Then I have the below simple code:
library(lokern)
model_np<-lokerns(y~x, data=data_test)
After executing the below code for getting estimates of y:
> predict(model_np,data_test$x)
$x
[1] 0.1570512 93.5554103 36.4963909 -3.5661127 34.7541054 -62.9400893 12.0450577 -35.6331874 -39.8288668 55.4826490 -184.9628427 18.4531852 24.0251748
[14] -17.2227224 72.0898450 77.3400270 11.4445439 -39.8399136 18.5753251 -91.0200934 -53.7306953 7.8290271 -81.2767392 86.2556579 -14.7859426 25.3677870
[27] -32.6501861 99.8278848 34.9829942 73.0140475 -16.6109127 -32.7592336 74.8537874 -37.8227589 12.7268935 36.3066722 -104.5535744 82.2742695 -16.2766760
[40] -14.8270656 -69.9284468 -18.6364153 20.2791386 -48.9278429 71.9757185 21.1260822 103.4247543 42.6072554 17.7123515 14.6981172 -16.6135251 -60.3311966
[53] 60.0246973 -25.8563621 9.8286898 -10.2896468 -64.5899488 74.4190914 -20.2832034 -41.4624966 22.6792451 62.3720212 43.0183778 -19.2322428 -103.9732312
[66] 8.6415758 -77.8111614 12.0402859 7.7602400 -29.4432033 169.3314805 20.2505791 11.6731976 -1.3726103 -24.2041204 -48.8151629 28.1533661 32.9620623
[79] 6.7930378 -41.4405364 61.2589756 -38.8352966 31.5222920 102.4265876 -8.3005717 3.6229150 -17.2961421 -128.8072678 -113.0477625 25.2152322 -18.7119726
[92] -169.8448571 -67.2163913 41.2838093 -39.2234895 -43.5948520 15.8241092 113.0589380 57.0302050 -159.7652908
$y
[1] 0.36009101 0.32846545 0.68153382 0.38608090 0.66311088 0.29850071 0.29482348 0.39911713 0.37939114 0.69109190 0.20305681 0.33928391 0.47218019 0.43021110 0.51132988 0.45387648
[17] 0.29602706 0.37935451 0.34154393 0.31747282 0.34292331 0.30097045 0.25197283 0.37870979 0.42909551 0.50262694 0.41085661 0.28147848 0.66571168 0.50020514 0.43004120 0.41047623
[33] 0.47934204 0.38850477 0.29338361 0.67971227 0.47394636 0.40898355 0.42994694 0.42913529 0.26796969 0.43009166 0.37838300 0.35720021 0.51272056 0.39929755 0.25286335 0.72536260
[49] 0.32675933 0.29689589 0.43004358 0.31165451 0.65366386 0.42507672 0.29829339 0.42021518 0.29158235 0.48410922 0.42917560 0.37334202 0.43945155 0.63039245 0.72679567 0.42969163
[65] 0.46593919 0.29918173 0.24064245 0.29483343 0.30118668 0.42028865 0.06706009 0.37770963 0.29558649 0.37134149 0.42644011 0.35747902 0.56081171 0.64072744 0.30540376 0.37341155
[81] 0.64169987 0.38367882 0.61990595 0.26089882 0.41260902 0.33069992 0.43023107 0.72733479 0.58602504 0.49925112 0.43004441 0.40246554 0.28114869 0.71925867 0.38195198 0.36833077
[97] 0.30465860 0.18813435 0.67963988 0.55540036
For example, for the value of x 0.157051, the estimated y value is 0.2626652.
Then, instead of using x as vector, I try to get estimates just only one input(x).
> predict(model_np,0.1570512)
$x
[1] 0.1570512
$y
[1] 0.2626652
For x=0.1570512 , I get estimate y=0.36009101 for the first one. But for the second one , I get y= 0.2626652. They are different from each other.
How can this be? Am I doing something wrong? Or is this something with versions of loaded packages? Are you getting the same results? Or is this a bug of the package? Or do i interpret result wrongly?
I will be very glad for any help. Thanks a lot.
lokern
is all over the place internally as it behaves differently if we are asking for anx
point it has already seen. It internally uses a spline interpolation if we are querying points we have not trained upon. That internal and conditional interpolation messes things spectacularly. Try:predict(model_np, x = data_test$x[1])
and then try:predict(model_np, x = data_test$x[1]+ 1e-16)
. $\endgroup$predict(model_np, x = data_test$x[1])
andpredict(model_np, x = c(0.123, data_test$x[1]))
will given different results fordata_test$x[1]
because in the later case the interpolator will kick in. Really, just use any other option,np::npreg
,stas::ksmooth
, something... $\endgroup$x
are not already see, the estimates from the kernel regression will go through a spline smooth. And that's why we get the difference. $\endgroup$