how do I select points in a dataset above x% contour of a density map? - r

I have a matrix of data (see below) and I am trying to turn it into a density contour map (Can1 and Can2 variables), maybe with ks or sm packages.
My question is how do I select those points in the dataset which lie above (say) 80% contour of the density map?
Thanks
ID Can1 Can2
4 -12.3235137 -1.0788867664
1 -12.2949912 -0.9321009837
5 -12.2835123 -1.0164225574
2 -12.2571822 -0.7094457036
3 -12.2713779 -0.9908419863
10 -12.9870438 -1.0936405526
6 -12.7167605 -1.4620772026
7 -12.8193776 -1.0911349785
8 -12.9781963 -1.1762698594
9 -12.7983478 -1.3453369581
13 -14.0389948 0.2855210115
11 -14.0015922 0.1467552738
15 -14.0723604 0.0244576488
14 -14.0743560 0.1417245145
12 -13.9898266 0.0005437008
20 -6.5881994 0.5124980991
17 -6.1812321 0.6789584579
16 -6.4704200 0.5942317307
18 -6.6960456 0.5720874622
19 -6.1159788 0.5960966790
22 -2.4794887 2.5493267897
24 -2.4918040 2.7823374576
21 -2.5145044 2.5877290160
23 -2.5048371 2.4916280770
25 -2.5018765 2.8536302559
29 -0.1781852 2.0805229401
26 -0.1581308 2.0151355747
28 -0.2118605 1.9658284615
27 -0.4184119 2.0540218901
30 -0.2994573 2.0205573385
35 2.6254869 1.3858705991
31 2.3146430 1.3510499304
33 2.5346138 1.2524229847
34 2.3741699 1.3842499455
32 2.6008389 1.3446707509
37 3.0920503 1.5807032840
38 3.1559727 1.4924092104
36 3.1593556 1.5803284343
39 3.0801444 1.6031732981
40 3.2562384 1.5810975265
43 4.8414364 2.1539254215
41 4.7938193 2.1613978258
44 4.7919209 2.2151527426
42 4.9830802 2.2374622446
45 4.7629268 2.4217335005
46 5.5631728 0.9986762598
50 5.5250403 1.0549399894
48 5.5833619 1.1368625963
47 5.5660312 1.1881215490
49 5.6224256 1.1634998303
53 5.5536366 0.2513665533
54 5.5276808 0.2685455911
51 5.7103045 0.2193839293
52 5.6014729 0.2353172964
55 5.5959034 0.2447836618
56 5.1542133 0.6070006863
59 5.0043394 0.4518710615
58 5.2314146 0.5656457888
60 5.1318728 0.4771275341
57 5.3599822 0.4918185651
61 7.0235173 -0.2669136870
63 7.0216315 -0.0097862523
64 7.0521253 -0.2457722410
62 7.0150637 -0.1456269078
65 7.0729018 -0.3573952321
69 5.8115406 -1.4652084167
67 5.7624475 -1.4147564126
68 5.8692888 -1.4695783153
70 5.9088094 -1.4927034632
66 5.8400205 -1.4817447808
71 4.8586107 -1.3111515744
73 4.7198564 -1.2891991780
72 4.9153659 -1.4499710448
74 4.7653488 -1.2839433419
75 4.7754971 -1.4655359108
77 3.8955675 -7.0922887151
78 3.8338151 -7.1595858283
80 3.7255063 -7.2147373050
79 3.7367055 -7.3468877516
76 4.0166957 -7.1952570639

Calculate the 80% point. One way: y<- x[x > 0.8 * max(x)] (I'm assuming you wanted 80% of the max level, not the 80th percentile) .
Then plot y .

After a bit of searching I think it can be achieved using the kde2d function from the MASS package.

Related

How to visualize a layer onto a map with R?

I am trying to produce a map which would visualize the lu_effic on my map. With the current codes I can visualize the map and the lu_effic category, but they are not layered on each other. I would like to fill the map with lu_effic colors. I tried with aes to add fill in geom_sf(data = shp_dt) but didn't work. The geom_polygon() also showed error, as the shape file data is a sf format. It has geometry column instead of longitude and latitude columns separately. Hence, failed to run aes (x= long, y=lat). I need to fill the map with lu_effic colors and other necessary visualization elements. Would anybody kindly assist me to solve this? Thanks in advance.
The data frame urb_dtf is here: https://docs.google.com/spreadsheets/d/1mZOkMTLx830oPaqtg9TVpg0OTP5fGzcR/edit?usp=sharing&ouid=114933701557583416369&rtpof=true&sd=true
urb_dtf
dtafil.GCPNT_LAT dtafil.GCPNT_LON lu_effic dtafil.UC_NM_MN
1 26.23825 88.40822 2.49722588 Atwari
2 26.03490 88.44599 1.30779927 Thakurgaon
3 26.33849 88.55293 4.41597618 Panchagarh
4 25.86100 88.36024 0.60699713 Pirganj
5 24.94617 88.21594 0.15688455 Asia
6 26.39944 88.96166 1.70447128 Asia
7 25.85807 88.65385 4.35869046 N/A
8 24.61404 88.23778 0.69757530 Chapainawabganj
9 25.62459 88.62559 0.70376589 Dinajpur
10 26.34469 89.01786 -0.78882643 Patgram
11 25.93154 88.85591 1.17247204 Nilphamari
12 25.79222 88.89938 0.53713622 Saidpur
13 24.44356 88.34488 21.83406438 Godagari
14 25.67936 89.05180 0.94306324 Bodorganj
15 24.41426 88.61983 0.67571401 Rajshahi
16 25.10153 89.02355 9.89191877 Joypurhat
17 24.80587 88.95890 1.26512380 Naogaon
18 24.37298 88.76614 0.43754723 Baneshwar
19 23.78359 88.63357 0.74975530 Meherpur
20 24.41677 88.99007 0.82625124 নাটোর
21 25.69513 89.60984 1.43875426 Ulipur
22 24.94282 89.37863 1.15731210 Bogura
23 25.32976 89.53690 1.28528943 Gaibandha
24 23.63560 88.83938 0.88394331 Chuadanga
25 24.08493 89.09479 0.36944300 Ishurdi
26 23.75310 88.94598 0.52944470 Alamdanga
27 23.90058 89.10053 0.68007198 Barkhada
28 24.65859 89.44197 1.51617122 Bogra
29 24.01567 89.26079 0.71299373 Pabna
30 23.86184 89.15752 0.82472321 Alampur
31 24.41254 89.64289 0.95057900 Sirajganj
32 23.40571 89.01174 0.69460150 Kotchadpur
33 23.04288 88.89597 0.75806861 Asia
34 23.54037 89.17593 0.61603886 Jhenaidah
35 23.40988 89.13477 1.10069949 কালীগঞà§\u008dজ
36 24.96816 89.97172 0.46869564 Jamalpur
37 23.16668 89.20843 0.52640165 Jessore
38 23.48845 89.42102 1.09687780 Magura
39 24.32668 89.94591 0.82200687 Tangail
40 24.76605 90.25557 0.56361005 Muktagacha
41 22.84015 89.55533 -1.26805716 Khulna
42 23.58577 89.83287 1.35248758 Faridpur
43 24.74904 90.38985 0.17224254 Mymensingh
44 23.84481 90.00978 0.95263604 Manikgonj
45 24.53342 90.34734 0.46884662 Trishal
46 23.80705 90.40166 0.17146435 Dhaka
47 23.91039 90.06940 0.25197463 Jaigir
48 24.54544 90.42188 0.26382334 Rampur
49 24.42903 90.38683 0.77181766 Bhaluka
50 24.49606 90.49881 0.32848142 Gafargaon
51 23.01082 89.82701 -2.48743996 Gopalganj
52 22.66161 89.77432 -0.26417887 Bagerhat
53 24.87865 90.72984 0.02135679 Netrokona
54 23.10274 90.17751 -2.59794615 Madaripur
55 24.41027 90.81461 0.29627310 Kishoreganj
56 24.57433 90.89773 0.40133470 Tarail
57 23.21486 90.33779 1.49758668 Shariatpur
58 23.55846 90.60338 0.84538204 N/A
59 24.05720 90.98408 0.15528478 Brahmanbaria
60 23.88657 90.73217 0.33951829 N/A
61 23.55013 90.66072 1.08523717 Gazaria
62 23.93776 90.86446 0.26901075 N/A
63 23.69415 90.83184 0.35016879 Homna
64 22.70696 90.35247 -0.89130028 Barisal
65 23.14673 90.80557 0.57463630 Chandpur
66 25.06408 91.40035 0.13625843 Sunamganj District
67 23.46517 90.78352 0.75471736 N/A
68 23.80230 90.97777 0.18687093 Nabinagar
69 23.89096 91.03594 0.05931816 Shibpur
70 24.06237 91.17975 0.09915729 Shahbazpur Town
71 22.99137 90.86482 0.19705905 Lakshmipur
72 22.67565 90.65828 1.14616972 Bhola
73 24.38139 91.41250 0.16456846 Habiganj
74 24.87859 91.90338 0.11481848 Sylhet
75 24.49782 91.78539 0.13333066 Moulvibazar
76 24.33299 91.73140 0.20055411 Sreemangal
77 24.82160 92.16267 0.18369562 Beanibazar
78 23.00552 91.40018 0.53240186 Feni
79 22.43880 91.82426 0.05366313 Chattogram
80 23.10985 91.98522 0.13909249 Khagrachhari
81 22.52012 91.92949 0.05425749 Raujan
82 21.75023 92.05997 0.11659127 Chakaria
83 22.20431 92.21605 0.08462762 Bandarban
84 21.43620 92.02313 0.07814110 Ramu
85 20.86985 92.29864 0.17803279 Teknaf
The shape file link: https://biogeo.ucdavis.edu/data/diva/adm/BGD_adm.zip
The Zip file is available at DIVA-GIS (a popular platform to download shape file). DIVA-GIS-> free spatial data -> country level data
The codes I ran are as follows:
library(sf)
library(ggplot2)
library(tidyverse)
urb_dt= read.csv("GHS_STAT_V1_2_BGD.csv")
urb_dtf= data.frame(dtafil$GCPNT_LAT, dtafil$GCPNT_LON, lu_effic, dtafil$UC_NM_MN)
dput(urb_dtf)
shp_dt= st_read("bgd_admbnda_adm3_bbs_20201113.shp")
str(shp_dt)
dput(shp_dt)
ggplot()+ geom_sf(data = shp_dt)+
geom_polygon(data = urb_dtf, aes(dtafil.GCPNT_LON, dtafil.GCPNT_LAT, group = lu_effic, fill = lu_effic))
Finally, I get the following image. But I would like to fill the map with the colors of lu_effic.
1

when you need a Kinhom rather than a Kest?

envelope of the K funcition (and its derivative such as L) is very useful for validating a fitted spatial points process model. for instance, I fit a poisson model for a data J1a2, which is as following:
J1a2.points:
# X.1 X Y
1 1 118.544 1638.445
2 2 325.995 1761.223
3 3 681.625 1553.771
4 4 677.392 1816.261
5 5 986.451 1685.016
6 6 1469.093 1354.787
7 7 1608.805 1625.744
8 8 1994.071 1782.391
9 9 1968.669 1375.955
10 10 2362.403 1337.852
11 11 2701.099 1773.924
12 12 2900.083 1820.495
13 13 2963.588 1668.081
14 14 3412.360 1676.549
15 15 3378.490 1456.396
16 16 3721.420 1464.863
17 17 3823.028 1701.951
18 18 4072.817 1790.859
19 19 4089.751 1388.656
20 20 97.375 715.497
21 21 376.799 1033.025
22 22 563.082 1126.166
23 23 935.647 1206.607
24 24 512.277 486.876
25 25 935.647 757.834
26 26 1409.821 410.670
27 27 1435.223 639.290
28 28 1706.180 1045.726
29 29 1968.669 876.378
30 30 2307.365 711.263
31 31 2624.892 897.546
32 32 2654.528 1236.243
33 33 2857.746 423.371
34 34 3039.795 639.290
35 35 3298.050 707.029
36 36 3111.767 1011.856
37 37 3361.555 1227.775
38 38 4047.414 1185.438
39 39 3569.007 508.045
40 40 4250.632 469.942
41 41 4386.110 872.144
42 42 93.141 237.088
43 43 554.614 186.283
44 44 757.832 148.180
45 45 965.283 220.153
46 46 1723.115 296.360
47 47 1744.283 423.371
48 48 1913.631 203.218
49 49 2167.653 292.126
50 50 2629.126 211.685
51 51 3217.610 283.658
52 52 3827.262 325.996
and:
J1a2.Win<-owin(c(0, 4500.42),c(0, 1917.87))
if you draw evelope for the data with Lest:
library(spatstat)
env.data<-envelope(J1a2, Lest,correction="border",
nsim=19, global=TRUE)
plot(env.data,.-r~r, shade=NULL, legend=FALSE,
xlab=expression(paste("r(",mu,"m)")),ylab="L(r)-r", main = "")
the Lest() curve goes out of the envelope. however, if you use Linhom instead of Lest, you will find the Linhom() are all inside of the envelope.
it seems that this suggest a inhomogenous density kernel of the data. so I use y as covariate in fitting:
poisson.J1a2<-ppm(J1a2~1,Poisson(),correction="border")
y1.J1a2<-ppm(J1a2~y,correction="border")
anova(poisson.J1a2,y.J1a2,test="LR") #p=0.6484
I don't find any evidence of a spatial trend of density along y, or x, or their combinations.
then why the Linhom() outperform the Lest() in this case?
furthermore, when should one decide to use Linhom() instead of Lest?
You should first decide whether or not the intensity can be assumed to be constant. To help you with this you can look at kernel density estimates or do formal tests such as a quadrat test etc. If you decide that the intensity can be assumed to be constant you use Lest() if this is not the case you use Linhom().

How to perform interpolation for a (column vector) data series using the largest column vector in a data frame in R

I have an excel sheet having some series of data in the form of column vectors. each column vector is of different length. the sample data in the excel sheet is presented as column vectors as shown below.
No 1 2 4 5 6 7
1 7.68565 7.431991 7.620156 7.34955 7.493848 7.244905
2 8.247334 7.895186 8.107751 7.629121 8.01165 7.898938
3 8.861417 8.411331 8.616113 7.960177 8.551065 8.432346
4 9.522981 8.945542 9.117843 8.263698 9.129371 9.118917
5 10.10206 9.465829 9.621576 8.515904 9.680468 9.695693
6 10.74194 10.05058 10.2111 8.824739 10.22375 10.48411
7 11.41614 10.59113 10.70612 9.12775 10.78299 11.1652
8 12.08601 11.12069 11.23061 9.445629 11.32874 11.8499
9 12.8509 11.68692 11.81479 9.762563 11.92125 12.77563
10 13.79793 12.31746 12.3436 10.12344 12.5586 14.05427
11 14.40335 12.85409 12.81579 10.4148 13.2323 14.74745
12 14.96397 13.44764 13.39124 10.76968 13.91571 15.48449
13 15.49457 13.5184 13.94058 11.05081 14.43318 16.12423
14 16.06153 13.99386 14.35261 11.38416 14.95082 16.84513
15 16.61133 14.4879 14.86438 11.71484 15.47574 17.42593
16 17.24876 14.95296 15.30651 12.06838 16.01853 18.05138
17 17.8686 15.48764 15.82241 12.41315 16.546 18.69939
18 18.49424 16.01478 16.33324 12.76782 17.07923 19.29467
19 19.0651 16.5115 16.8808 13.11234 17.62211 20.00391
20 19.73842 17.07482 17.40481 13.46479 18.14528 20.67474
21 20.47123 17.51353 17.88455 13.55012 18.69565 21.35446
22 21.16333 18.00172 18.38069 13.82592 19.23222 22.16516
23 21.83083 18.55357 18.79004 14.10343 19.93576 23.0249
24 22.50095 19.04932 19.25296 14.38997 20.6087 23.75609
25 23.27895 19.66359 19.68497 14.66933 21.19856 24.33014
26 23.86791 20.19746 20.25114 14.96252 21.7933 25.16132
27 24.42128 20.79322 20.8394 15.27082 22.4216 25.64038
28 25.02747 21.34963 21.36803 15.59645 22.95553 26.40612
29 25.64392 21.96625 21.92369 15.90159 23.62858 26.99359
30 26.15457 22.51419 22.49119 16.21841 24.27062 27.48933
31 26.78083 23.14052 23.09582 16.5353 24.75912 28.13525
32 27.39095 23.71215 23.71597 16.84909 25.34079 28.66253
33 28.04546 24.23099 24.22622 17.23782 25.90887 29.27824
34 28.68887 24.69722 24.76757 17.58071 26.51803 30.06892
35 29.45707 25.24266 25.30781 17.91193 27.12488 30.87034
36 30.03946 25.75705 25.86998 18.24291 27.73606 31.71053
37 30.71511 26.29254 26.34333 18.50986 28.30462 32.37958
38 31.42378 26.91853 26.69165 18.81327 28.91142 33.07085
39 32.50335 27.44403 27.12134 19.20657 29.51637 33.8685
40 33.12328 27.98299 27.578 19.55173 30.14371 34.5783
41 33.71293 28.42661 28.16382 19.818 30.7509 35.29098
42 34.22313 29.11766 28.58075 20.20322 31.50584 35.97233
43 34.84822 29.69339 29.14229 20.60828 32.14028 36.53085
44 35.51228 30.30699 29.71523 20.86474 32.72842 36.82623
45 36.11674 30.89355 30.28881 21.24548 33.02594 37.79391
46 36.80722 31.50952 30.94186 21.56593 33.17226 38.42553
47 37.60966 31.98561 31.63391 21.89768 33.34089 39.20039
48 38.25016 32.63639 32.19883 22.23119 33.67384 39.98531
49 38.95744 33.18134 32.72147 22.4859 34.27073 40.76857
50 39.66163 33.67109 33.14864 22.90394 34.86681 41.49251
51 40.37425 34.12463 33.60807 23.26918 35.59697 42.51444
52 41.23707 34.66628 34.09723 23.52158 36.24535 43.14603
53 41.82558 35.1961 34.57659 23.89679 36.90796 44.16233
54 42.55081 35.72951 35.03618 24.49229 37.65297 44.59068
55 43.39907 36.31952 35.46371 24.81181 38.33818 45.22966
56 44.05056 37.05194 35.98615 25.12065 38.85623 46.23367
57 44.78049 37.1323 36.51719 25.4582 39.54339 46.54872
58 45.43282 37.76535 37.09313 25.88998 40.23827 47.07784
59 46.18882 38.27575 37.17476 26.22639 40.92604 47.807
60 46.90982 38.88576 37.90604 26.56257 41.63398 48.4778
61 47.56264 39.64927 38.5283 26.8499 42.29979 49.21885
62 48.10035 40.19561 39.16806 27.1614 42.99679 50.18735
63 49.01068 40.89077 39.80176 27.43677 43.8278 51.9102
64 49.76271 41.6514 40.39578 27.89204 44.4915 52.78747
65 50.53434 42.09778 41.03402 28.18638 45.01828 53.46253
66 51.67479 42.83619 41.44307 28.49254 45.8151 54.44443
67 52.20818 43.35224 42.17046 28.87821 46.38069 55.20507
68 52.84818 43.94838 42.54818 29.18387 47.27983 55.71156
69 53.54274 44.61937 43.04368 29.58712 47.76875 56.11357
70 54.24117 45.2113 43.55424 29.97786 48.52082 56.56269
71 55.10781 45.87016 44.19418 30.30342 49.17041 57.04574
72 55.81844 46.58728 44.70245 30.92939 50.00576 57.61847
73 56.53417 47.17022 45.19135 64.12819 50.76387 58.46774
74 56.99077 47.80587 45.81162 64.46482 51.44632 59.35406
75 57.70125 48.4632 46.53608 64.47179 52.09271 60.34232
76 58.40646 49.11251 47.44626 65.28538 52.77505 60.76057
77 59.20803 49.70755 48.0586 65.42728 53.3777 61.86707
78 59.71753 50.13534 48.76304 65.97044 54.06384 63.14102
79 60.58331 50.72049 49.47997 66.51449 54.7547 64.43312
80 61.03398 51.41927 50.11546 67.02634 55.4798 65.58254
81 61.80681 51.97609 50.69514 67.59518 55.96139 66.72086
82 62.48501 52.59973 51.31683 68.12712 56.93643 67.53484
83 63.36452 53.36562 51.73617 68.64816 57.6551 68.07806
84 64.31261 53.98405 52.21327 69.24711 58.23373 68.63623
85 65.24776 54.51552 52.77048 70.48085 58.97933 69.02074
86 66.17772 55.20282 53.22162 70.64199 59.76285 69.38057
87 67.08787 55.91391 53.7916 71.38781 60.25809 70.01195
88 68.01987 56.61301 54.46721 71.58064 61.31948 70.5335
89 68.92189 57.28238 55.16064 71.99983 62.18978 71.61938
90 69.79762 57.88332 55.85772 72.89091 63.02894 72.77907
91 69.86632 58.52047 56.78106 73.05919 63.78964 74.13258
92 70.60662 59.12164 57.49112 73.58095 64.54343 75.77073
93 71.63203 59.77399 58.20212 74.1192 65.36834 76.57243
94 72.18227 60.47282 58.77127 74.6143 65.83804 77.84715
95 72.97624 60.7739 59.41283 75.4809 66.61507 78.78102
96 73.75372 61.22352 59.84708 75.66663 67.44336 79.33527
97 74.66983 61.87689 60.49374 76.09998 68.30974 79.86294
98 75.85329 62.58495 60.7886 76.67287 69.23421 80.51763
99 76.38837 63.32424 61.5629 77.20351 70.00735 80.91219
100 77.38139 64.07433 62.21648 77.95189 70.7836 81.57964
101 78.25631 64.82328 62.74316 78.21231 71.2177 82.16656
102 79.19827 65.50484 63.64724 78.89301 72.00792 83.12364
103 80.38764 66.23685 64.48991 79.32261 73.00548 84.00261
104 80.87278 66.95412 65.2793 79.95379 73.50331 85.22213
105 81.76581 67.70247 65.82581 80.52102 74.28909 86.6621
106 83.02712 68.55701 66.62666 81.06393 75.11777 88.11059
107 83.48909 69.23235 67.35486 81.7409 75.9652
108 84.82759 70.58522 68.15342 82.25188 76.8884
109 85.28537 71.04559 68.92251 82.98396 77.83717
110 86.70018 71.73407 69.51888 83.51862 78.45438
111 87.35397 72.45837 70.31539 83.69946 79.32315
112 88.69969 73.14394 70.9007 84.25947 80.39831
113 73.92206 71.50578 85.10349 81.20853
114 74.65082 72.20686 85.26869 81.95338
115 75.32388 72.81664 86.07426 82.36201
116 76.37313 73.52561 86.33713 83.16817
117 76.85229 74.32013 86.85325 83.96463
118 77.55033 75.04207 87.32344 84.8136
119 78.19957 75.90256 87.93314 85.7303
120 79.23823 76.41772 88.39268 86.46136
121 79.57755 77.11913 88.96714 87.30937
122 79.70834 78.01459 88.17579
123 80.44374 78.76607 89.00109
124 81.47443 79.56496
125 81.80569 79.69939
126 82.57823 80.52383
127 83.38485 81.27236
128 84.09743 81.94386
129 84.78618 83.01913
130 85.91491 83.52692
131 86.18631 84.52093
132 86.87262 85.26204
133 88.0145 85.93992
134 88.30018 86.70402
135 89.08487 87.58891
136 88.27903
from the above data, the values are ranged from 7.3 (approx.) to 89.08 (approx) in the top to bottom. however, I have some data ranged from 7.3 to 89.09 (approx) in the bottom to top in another sheet of excel file.
Now, I would like to take the longest column vector (from the sample data it is column vector: 3) i.e 136*1 size and convert other column vectors (1,2,4,5 and 6) into the size of column vector :3 such that the original values (magnitudes) should remain same and their positions (values in the rows can be shifted). Between the values (original magnitudes), I need to interpolate so that, all the column vectors will be of same length (136*1).
like this column vectors, I have some hundreds.
the expected output is presented only for column:1 with reference to column:3
No 1 3
1 7.68565 7.620156
2 8.247334 8.107751
3 8.861417 8.616113
4 9.522981 9.117843
5 **9.8125205** 9.621576
6 10.10206 10.2111
7 10.74194 10.70612
8 11.41614 11.23061
9 **11.751075** 11.81479
10 12.08601 12.3436
11 12.8509 12.81579
12 13.79793 13.39124
13 **14.10064** 13.94058
14 14.40335 14.35261
15 14.96397 14.86438
16 15.49457 15.30651
17 **15.77805** 15.82241
18 16.06153 16.33324
19 16.61133 16.8808
20 17.24876 17.40481
21 17.8686 17.88455
22 18.49424 18.38069
23 **18.77967** 18.79004
24 19.0651 19.25296
25 19.73842 19.68497
26 20.47123 20.25114
27 **20.81728** 20.8394
28 21.16333 21.36803
29 21.83083 21.92369
30 22.50095 22.49119
31 23.27895 23.09582
32 23.86791 23.71597
33 24.42128 24.22622
34 **24.724375** 24.76757
35 25.02747 25.30781
36 25.64392 25.86998
37 26.15457 26.34333
38 26.78083 26.69165
39 27.39095 27.12134
40 **27.718205** 27.578
41 28.04546 28.16382
42 28.68887 28.58075
43 29.45707 29.14229
44 **29.748265** 29.71523
45 30.03946 30.28881
46 30.71511 30.94186
47 31.42378 31.63391
48 32.50335 32.19883
49 **32.813315** 32.72147
50 33.12328 33.14864
51 33.71293 33.60807
52 34.22313 34.09723
53 34.84822 34.57659
54 **35.18025** 35.03618
55 35.51228 35.46371
56 **35.81451** 35.98615
57 36.11674 36.51719
58 36.80722 37.09313
59 37.60966 37.17476
60 **37.92991** 37.90604
61 38.25016 38.5283
62 38.95744 39.16806
63 39.66163 39.80176
64 40.37425 40.39578
65 41.23707 41.03402
66 41.82558 41.44307
67 42.55081 42.17046
68 **42.97494** 42.54818
69 43.39907 43.04368
70 **43.724815** 43.55424
71 44.05056 44.19418
72 44.78049 44.70245
73 45.43282 45.19135
74 **45.81082** 45.81162
75 46.18882 46.53608
76 46.90982 47.44626
77 47.56264 48.0586
78 48.10035 48.76304
79 49.01068 49.47997
80 49.76271 50.11546
81 50.53434 50.69514
82 51.67479 51.31683
83 **51.941485** 51.73617
84 52.20818 52.21327
85 52.84818 52.77048
86 53.54274 53.22162
87 **53.891955** 53.7916
88 54.24117 54.46721
89 55.10781 55.16064
90 55.81844 55.85772
91 56.53417 56.78106
92 56.99077 57.49112
93 57.70125 58.20212
94 58.40646 58.77127
95 59.20803 59.41283
96 59.71753 59.84708
97 60.58331 60.49374
98 61.03398 60.7886
99 61.80681 61.5629
100 62.48501 62.21648
101 **62.924765** 62.74316
102 63.36452 63.64724
103 64.31261 64.48991
104 65.24776 65.2793
105 **65.71274** 65.82581
106 66.17772 66.62666
107 67.08787 67.35486
108 68.01987 68.15342
109 68.92189 68.92251
110 69.79762 69.51888
111 69.86632 70.31539
112 70.60662 70.9007
113 71.63203 71.50578
114 72.18227 72.20686
115 72.97624 72.81664
116 73.75372 73.52561
117 74.66983 74.32013
118 75.85329 75.04207
119 76.38837 75.90256
120 **76.88488** 76.41772
121 77.38139 77.11913
122 78.25631 78.01459
123 **78.72729** 78.76607
124 79.19827 79.56496
125 **79.792955** 79.69939
126 80.38764 80.52383
127 80.87278 81.27236
128 81.76581 81.94386
129 83.02712 83.01913
130 83.48909 83.52692
131 84.82759 84.52093
132 85.28537 85.26204
133 85.992775 85.93992
134 86.70018 86.70402
135 87.35397 87.58891
136 88.69969 88.27903
the expected interpolated values in column:1 are presented in double starred. here, the interpolation is done by averaging the i-1th and i+1th cell for the ith cell (simply linear interpolation)
the main purpose of doing so is to perform clustering. since column vectors/row vectors of unequal length cannot be used for clustering
is there any code to do that?
or can we calculate distance using DTW(Dynamic Time Warping) method or any other method with column vectors having unequal length (as shown in the example dataset) and perform clustering??

Calculate mean value for each row with interval

i need to calculate the mean value for each row (mean of interval). Here is a basic example (maybe anyone has even better idea to do it):
M_1_mb <- (15 : -15)#creating a vector value --> small
M_31 <- cut(M_31_mb,128)# getting 128 groups from the small vector
#M_1_mb <- (1500 : -1500)#creating a vector value
#M_1 <- cut(M_1_mb,128)# getting 128 groups from the vector
I do need to get the mean value for each row/group out of 128 intervals created in M_1 (actually i do not need even those intervals, i just need the mean of them) and i cannot figure out how to do it...
I had a look at the cut2 function from Hmisc library but unfortunatelly there is no option to set up number of intervals into which vector is to be cut (-> but there is an option to get the mean value of created intervals: levels.mean...)
I would appreciate any help! Thanks!
Additional Info:
cut2 function is working well for bigger vectors (M_1_mb), however when my vector is small (M_31_mb), then i am getting a Warning message:
Warning message:
In min(xx[xx > upper]) : no non-missing arguments to min; returning Inf
and only 31 groups are created:
M_31_mb <- (15 : -15) # smaller vector
M_31 <- table(cut2(M_31_mb,g=128,levels.mean = TRUE))
whereas
g = number of quantile groups
like this?
aggregate(M_1_mb,by=list(M_1),mean)
EDIT: Result
Group.1 x
1 (-1.5e+03,-1.48e+03] -1488.5
2 (-1.48e+03,-1.45e+03] -1465.0
3 (-1.45e+03,-1.43e+03] -1441.5
4 (-1.43e+03,-1.41e+03] -1418.0
5 (-1.41e+03,-1.38e+03] -1394.5
6 (-1.38e+03,-1.36e+03] -1371.0
7 (-1.36e+03,-1.34e+03] -1347.5
8 (-1.34e+03,-1.31e+03] -1324.0
9 (-1.31e+03,-1.29e+03] -1301.0
10 (-1.29e+03,-1.27e+03] -1277.5
11 (-1.27e+03,-1.24e+03] -1254.0
12 (-1.24e+03,-1.22e+03] -1230.5
13 (-1.22e+03,-1.2e+03] -1207.0
14 (-1.2e+03,-1.17e+03] -1183.5
15 (-1.17e+03,-1.15e+03] -1160.0
16 (-1.15e+03,-1.12e+03] -1136.5
17 (-1.12e+03,-1.1e+03] -1113.0
18 (-1.1e+03,-1.08e+03] -1090.0
19 (-1.08e+03,-1.05e+03] -1066.5
20 (-1.05e+03,-1.03e+03] -1043.0
21 (-1.03e+03,-1.01e+03] -1019.5
22 (-1.01e+03,-984] -996.0
23 (-984,-961] -972.5
24 (-961,-938] -949.0
25 (-938,-914] -926.0
26 (-914,-891] -902.5
27 (-891,-867] -879.0
28 (-867,-844] -855.5
29 (-844,-820] -832.0
30 (-820,-797] -808.5
31 (-797,-773] -785.0
32 (-773,-750] -761.5
33 (-750,-727] -738.0
34 (-727,-703] -715.0
35 (-703,-680] -691.5
36 (-680,-656] -668.0
37 (-656,-633] -644.5
38 (-633,-609] -621.0
39 (-609,-586] -597.5
40 (-586,-562] -574.0
41 (-562,-539] -551.0
42 (-539,-516] -527.5
43 (-516,-492] -504.0
44 (-492,-469] -480.5
45 (-469,-445] -457.0
46 (-445,-422] -433.5
47 (-422,-398] -410.0
48 (-398,-375] -386.5
49 (-375,-352] -363.0
50 (-352,-328] -340.0
51 (-328,-305] -316.5
52 (-305,-281] -293.0
53 (-281,-258] -269.5
54 (-258,-234] -246.0
55 (-234,-211] -222.5
56 (-211,-188] -199.0
57 (-188,-164] -176.0
58 (-164,-141] -152.5
59 (-141,-117] -129.0
60 (-117,-93.8] -105.5
61 (-93.8,-70.3] -82.0
62 (-70.3,-46.9] -58.5
63 (-46.9,-23.4] -35.0
64 (-23.4,0] -11.5
65 (0,23.4] 12.0
66 (23.4,46.9] 35.0
67 (46.9,70.3] 58.5
68 (70.3,93.8] 82.0
69 (93.8,117] 105.5
70 (117,141] 129.0
71 (141,164] 152.5
72 (164,188] 176.0
73 (188,211] 199.0
74 (211,234] 222.5
75 (234,258] 246.0
76 (258,281] 269.5
77 (281,305] 293.0
78 (305,328] 316.5
79 (328,352] 340.0
80 (352,375] 363.5
81 (375,398] 387.0
82 (398,422] 410.0
83 (422,445] 433.5
84 (445,469] 457.0
85 (469,492] 480.5
86 (492,516] 504.0
87 (516,539] 527.5
88 (539,562] 551.0
89 (562,586] 574.0
90 (586,609] 597.5
91 (609,633] 621.0
92 (633,656] 644.5
93 (656,680] 668.0
94 (680,703] 691.5
95 (703,727] 715.0
96 (727,750] 738.5
97 (750,773] 762.0
98 (773,797] 785.0
99 (797,820] 808.5
100 (820,844] 832.0
101 (844,867] 855.5
102 (867,891] 879.0
103 (891,914] 902.5
104 (914,938] 926.0
105 (938,961] 949.0
106 (961,984] 972.5
107 (984,1.01e+03] 996.0
108 (1.01e+03,1.03e+03] 1019.5
109 (1.03e+03,1.05e+03] 1043.0
110 (1.05e+03,1.08e+03] 1066.5
111 (1.08e+03,1.1e+03] 1090.0
112 (1.1e+03,1.12e+03] 1113.5
113 (1.12e+03,1.15e+03] 1137.0
114 (1.15e+03,1.17e+03] 1160.0
115 (1.17e+03,1.2e+03] 1183.5
116 (1.2e+03,1.22e+03] 1207.0
117 (1.22e+03,1.24e+03] 1230.5
118 (1.24e+03,1.27e+03] 1254.0
119 (1.27e+03,1.29e+03] 1277.5
120 (1.29e+03,1.31e+03] 1301.0
121 (1.31e+03,1.34e+03] 1324.0
122 (1.34e+03,1.36e+03] 1347.5
123 (1.36e+03,1.38e+03] 1371.0
124 (1.38e+03,1.41e+03] 1394.5
125 (1.41e+03,1.43e+03] 1418.0
126 (1.43e+03,1.45e+03] 1441.5
127 (1.45e+03,1.48e+03] 1465.0
128 (1.48e+03,1.5e+03] 1488.5

Saving an output from R into excel format?

After running the predict function for glm i get an output in the below format:
1 2 3 4 5 6 7 8 9 10 11 12
3.954947e-01 8.938624e-01 7.775473e-01 1.294646e-02 3.954947e-01 9.625746e-01 9.144256e-01 4.739872e-01 1.443219e-01 1.180850e-04 2.138978e-01 7.775473e-01
13 14 15 16 17 18 19 20 21 22 23 24
5.425436e-03 2.069844e-04 2.723969e-01 4.739872e-01 9.144256e-01 1.091998e-01 2.070056e-02 5.114936e-01 1.443219e-01 5.922029e-01 7.578099e-02 8.937642e-01
25 26 27 28 29 30 31 32 33 34 35 36
6.069970e-02 6.069970e-02 1.337947e-01 1.090992e-01 4.841467e-02 9.205547e-01 3.954947e-01 3.874915e-05 3.855242e-02 1.344839e-01 6.318574e-04 2.723969e-01
37 38 39 40 41 42 43 44 45 46 47 48
7.400276e-04 8.593199e-01 6.666800e-01 2.069844e-04 8.161623e-01 4.916555e-05 3.060374e-02 3.402079e-01 2.256598e-03 9.363767e-01 6.116082e-01 3.940969e-03
49 50 51 52 53 54 55 56 57 58 59 60
7.336723e-01 2.425257e-02 3.369967e-03 5.624262e-02 1.090992e-01 1.357630e-06 1.278169e-04 3.046189e-01 8.938624e-01 4.535894e-01 5.132348e-01 3.220426e-01
61 62 63 64 65 66 67 68 69 70 71 72
3.366492e-03 1.357630e-06 1.014721e-01 1.294646e-02 9.144256e-01 1.636988e-02 2.070056e-02 1.012835e-01 5.000274e-03 8.165247e-02 1.357630e-06 8.033850e-03
IS there any code by which I can get the complete output vertically or in an excel format? Thank you in advance!
The simplest way is to write a character separated value file using a comma as the delimiter:
[Acknowledge Roland's comment] write.csv(data.frame(predict(yourGLM)), "file.csv")
Excel reads these automatically, especially if you save the file with a .csv extension.
If its just a matter of viewing it vertically first create the data:
# create test data
example(predict.glm)
pred <- predict(budworm.lg)
1) Separate R Window Use View to display it in a separate R window:
View(pred)
2) R Console to display it on the R console vertically:
data.frame(pred)
3) Browser to display it in the browser vertically:
library(R2HTML)
HTMLStart(); HTML(data.frame(pred)); w <- HTMLStop()
browseURL(w)
4) Excel to display it in Excel vertically using w we just computed:
shell(paste("start excel", w))

Resources