Related
I want to replace the points in my graph with a line like in the first picture, the second picture is what I have.
but its not quite what im looking for, I want a smooth line without the points
I think I have to use predict for the 1/x curve but I am not sure how,
Assuming f(1/x) fits the data well. One can use the lm() function to fix the desired function y= a/x + b and then use the predict() function to estimate the desired points.
If a more complicated nonlinear function is required to fit the data then the nls() maybe required
x<- c(176.01685819061, 21.6704613594849, 19.007554742708, 50.1865574864131, 17.6174002411188, 40.2758022496774, 11.0963214407251, 1249.94375253114, 694.894678288085, 339.786950220117, 42.1452961176151, 220.352895161601, 19.6303352674776, 9.10350287678884, 10.6222946396451, 44.1984352318898, 21.8069112975004, 42.1237630342764, 22.7551891190248, 12.9587850506626, 12.0207189111152, 20.2704921282476, 13.3441156357956, 9.13092569988769, 1781.08346869568, 71.2690023512206, 80.2376892286713, 344.114362037227, 208.830841645638, 91.1778810401913, 2220.0120768657, 41.4820962277111, 16.5730025748281, 32.30173229022, 108.703930214512, 51.6770035143256, 709.071405759588, 87.9618878732223, 10.4198968123037, 34.4951840238729, 57.8603720445067, 72.3289197551429, 30.2366643066749, 23.8696161364716, 270.014690419247, 13.8170113452005, 39.5159584479013, 27.764841260433, 18.0311836472615, 40.5709477295999, 33.1888820958952, 9.03112843931787, 4.63738971549635, 12.7591169313099, 4.7998894219979, 8.93458248803248, 7.33904760386628, 12.0940344070925, 7.17364602165948, 6.514191844409, 9.69911157978057, 6.57874454980745, 7.90556524435596)
y<- c(0.02840637, 0.230728821, 0.2630533, 0.099628272, 0.28381032, 0.12414402, 0.45059978, 0.00400018, 0.00719533500000001, 0.014715103086687, 0.118637201789886, 0.022690875, 0.254707825, 0.54923913, 0.470708088, 0.113126176837872, 0.22928510745, 0.118697847481752, 0.219730100850697, 0.38583864, 0.4159485, 0.24666396693114, 0.374696992776912, 0.547589605297248, 0.00280728, 0.070156727820596, 0.062314855376136, 0.01453005323695, 0.02394282358199, 0.0548378613646, 0.00225224, 0.120533928, 0.301695482, 0.15479046, 0.045996497, 0.096754836, 0.00705147600000001, 0.0568428, 0.47985120103071, 0.14494777, 0.08641493, 0.069128642, 0.165362156, 0.20947132, 0.018517511, 0.36187275779699, 0.126531158458224, 0.180083867690804, 0.277297380904852, 0.1232408972382, 0.15065285976048, 0.55364067, 1.07819275643191, 0.39187665, 1.04169066418176, 0.55962324, 0.68128731, 0.41342697, 0.69699564, 0.76755492, 0.515511133042674, 0.760023430328564, 0.632465844687028)
#data frame for prediction
df <- data.frame(x=sort(x))
# fit model y= a/x + b
model <-lm( y ~ I(1/x))
#summary(model)
#plot model
plot(df$x, predict(model, df), type="l", col="blue")
#optional
points(x, y)
Update - response to comments
x is sorted in the data frame, so that points are plotted in order. If not the line could go from x=1 to x=100, back to x=10 etc. thus making a mess. Try removing the sort and see what happens.
The I(1/x) term is to signal lm to perform the inverse transform on x first and then perform the least squares regression.
The predict() function is on the axis since that is the variable used in the plot function. To change this just assign the output from the predict function to a better variable name and plot that. Or use the "ylab= " option.
For smoothing, you can fit a linear model as foolws:
m <- lm(AM_cost_resorb~I(1/AM_leafP), data=data)
Then extract the predictied values on a new data set that covers the range of the exposure variable.
newx <- seq(min(data$AM_leafP), max(data$AM_leafP), by=0.01)
pr <- predict(m, newdata=data.frame(AM_leafP=newx))
And visualize:
plot(AM_cost_resorb~AM_leafP, data=data, type="p", pch= 15, col="red",ylab="Cost of reabsorbtion (kg C m^-2 yr^-1)", xlab="leaf P before senescence (g P/m2)", ylim=c(0,500), las=1)
lines(newx, y=pr, col="blue", lwd=2)
Data:
data <- structure(list(AM_cost_resorb = c(176.01685819061, 21.6704613594849,
19.007554742708, 50.1865574864131, 17.6174002411188, 40.2758022496774,
11.0963214407251, 1249.94375253114, 694.894678288085, 339.786950220117,
42.1452961176151, 220.352895161601, 19.6303352674776, 9.10350287678884,
10.6222946396451, 44.1984352318898, 21.8069112975004, 42.1237630342764,
22.7551891190248, 12.9587850506626, 12.0207189111152, 20.2704921282476,
13.3441156357956, 9.13092569988769, 1781.08346869568, 71.2690023512206,
80.2376892286713, 344.114362037227, 208.830841645638, 91.1778810401913,
2220.0120768657, 41.4820962277111, 16.5730025748281, 32.30173229022,
108.703930214512, 51.6770035143256, 709.071405759588, 87.9618878732223,
10.4198968123037, 34.4951840238729, 57.8603720445067, 72.3289197551429,
30.2366643066749, 23.8696161364716, 270.014690419247, 13.8170113452005,
39.5159584479013, 27.764841260433, 18.0311836472615, 40.5709477295999,
33.1888820958952, 9.03112843931787, 4.63738971549635, 12.7591169313099,
4.7998894219979, 8.93458248803248, 7.33904760386628, 12.0940344070925,
7.17364602165948, 6.514191844409, 9.69911157978057, 6.57874454980745,
7.90556524435596), AM_leafP = c(0.02840637, 0.230728821, 0.2630533,
0.099628272, 0.28381032, 0.12414402, 0.45059978, 0.00400018,
0.00719533500000001, 0.014715103086687, 0.118637201789886, 0.022690875,
0.254707825, 0.54923913, 0.470708088, 0.113126176837872, 0.22928510745,
0.118697847481752, 0.219730100850697, 0.38583864, 0.4159485,
0.24666396693114, 0.374696992776912, 0.547589605297248, 0.00280728,
0.070156727820596, 0.062314855376136, 0.01453005323695, 0.02394282358199,
0.0548378613646, 0.00225224, 0.120533928, 0.301695482, 0.15479046,
0.045996497, 0.096754836, 0.00705147600000001, 0.0568428, 0.47985120103071,
0.14494777, 0.08641493, 0.069128642, 0.165362156, 0.20947132,
0.018517511, 0.36187275779699, 0.126531158458224, 0.180083867690804,
0.277297380904852, 0.1232408972382, 0.15065285976048, 0.55364067,
1.07819275643191, 0.39187665, 1.04169066418176, 0.55962324, 0.68128731,
0.41342697, 0.69699564, 0.76755492, 0.515511133042674, 0.760023430328564,
0.632465844687028)), class = "data.frame", row.names = c(NA,
-63L))
I am wanting to find the point of intercept between my Stress-Strain curve from a tensile test and the 0.2% offset line.
I am using plot() and lines() to graph this curve and line. The Stress-Strain curve is imported from a csv that contains the raw test data for stress (y axis) and strain (x axis). Below is the code snippet
TH_1_frame <- data.frame(TH_1_Strain, TH_1_Stress) # the x and y data for stress strain curve
Offset <- 0.002
TH_1_Stress02 <- TH_1_Mod*1000*(TH_1_Strain - Offset) # TH_1_Mod is the elastic modulus and the gradient of the straight line
TH_1_Strain02 <- TH_1_Strain + Offset
TH_1_02frame <- data.frame(TH_1_Strain02, TH_1_Stress02) # the x and y for the 0.2% offset line
plot(TH_1_Strain, TH_1_Stress)
lines(TH_1_Strain02, TH_1_Stress02) # plotting the curve and line
Attached is the output of the plot, I am unsure how I would go about mathematically finding the intercept seen in this plot.
Thanks
Tried my best to find the data around this intercept. Can attach the csv if anyone would like.
dput( TH_1_Strain[9450:9500])
c(0.01567022254, 0.01566837231, 0.01566786692, 0.0156739677,
0.01566912234, 0.01566456631, 0.01567340766, 0.01567904775, 0.01568320269,
0.0156837975, 0.01568157723, 0.01568231732, 0.0156824626, 0.01568279291,
0.01568822314, 0.01569599782, 0.01569751774, 0.01569260284, 0.01569438353,
0.01569401845, 0.01569528257, 0.01570160811, 0.01570275302, 0.01571064318,
0.01570881406, 0.01570606853, 0.01571034392, 0.01570852846, 0.01571317886,
0.01572433362, 0.01572758953, 0.01572479432, 0.01569923262, 0.01569651316,
0.01570669313, 0.01571742942, 0.01573171467, 0.01576093584, 0.01573084916,
0.01569425811, 0.01570926358, 0.01572924977, 0.01574603468, 0.01575124015,
0.01574295014, 0.0157436803, 0.0157436803, 0.01571707924, 0.01571890339,
0.01574868957, 0.01576487596)
>
> dput( TH_1_Stress[9450:9500])
c(785.800149043174, 785.821152259166, 785.839877152789, 785.856608787737,
785.871061735784, 785.884019739826, 785.896123281846, 785.908297907524,
785.918265659023, 785.930013418222, 785.937987546514, 785.943825519753,
785.953650739404, 785.966252960625, 785.976007096618, 785.989748114491,
786.001709124664, 786.012175190831, 786.021431012156, 786.030045986964,
786.039230360099, 786.050835587451, 786.059236946754, 786.065715766509,
786.072479649959, 786.076110749018, 786.079812931735, 786.083230415289,
786.085793436822, 786.087502360865, 786.087573444522, 786.084084877311,
786.080524861909, 786.079955463583, 786.078673770551, 786.079172085219,
786.078673770551, 786.078104372224, 786.075469902502, 786.07333411198,
786.072408566301, 786.075968217171, 786.081165708425, 786.085508737659,
786.09042152975, 786.091204543582, 786.09440841163, 786.099392287379,
786.112350291421, 786.12367081961, 786.138550998668)
Here I have another 'graphical' problem:
I have obtained from MOTHUR the following distance matrix (coming from a weighted unifrac analysis):
20
F3D0
F3D1 0.222664
F3D141 0.157368 0.293308
F3D142 0.180278 0.319198 0.0944511
F3D143 0.157659 0.290975 0.0545202 0.0761392
F3D144 0.199909 0.34045 0.104358 0.086418 0.089473
F3D145 0.207946 0.348532 0.107841 0.076302 0.0940067 0.051632
F3D146 0.117877 0.253996 0.0891617 0.130867 0.0882064 0.134407 0.138415
F3D147 0.197256 0.336583 0.102114 0.0764106 0.0890669 0.0514887 0.0479297 0.135324
F3D148 0.173824 0.311951 0.0606815 0.0648557 0.056463 0.074914 0.0811015 0.111996 0.0709027
F3D149 0.145614 0.276632 0.0462779 0.105512 0.0628737 0.10902 0.114584 0.0739466 0.107123 0.0690412
F3D150 0.129557 0.277624 0.0840909 0.128305 0.0863231 0.140256 0.145381 0.0744572 0.13672 0.113564 0.0659831
F3D2 0.133531 0.216587 0.160832 0.186833 0.176061 0.214934 0.215261 0.152591 0.205629 0.188325 0.156313 0.153841
F3D3 0.213102 0.305651 0.123818 0.113021 0.139376 0.148558 0.13853 0.174377 0.139851 0.126329 0.131294 0.166738 0.137784
F3D5 0.128668 0.185235 0.167733 0.205183 0.176585 0.224806 0.230984 0.14497 0.223492 0.18933 0.153624 0.148617 0.127574 0.192433
F3D6 0.139411 0.236633 0.135418 0.124848 0.134198 0.175098 0.166205 0.118905 0.166144 0.151842 0.120964 0.12724 0.0950943 0.119852 0.129523
F3D7 0.198884 0.315888 0.130385 0.0989168 0.131945 0.14625 0.126203 0.173689 0.128993 0.121373 0.140199 0.152123 0.152893 0.0906675 0.186674 0.111134
F3D8 0.178656 0.18783 0.205737 0.22104 0.219858 0.268701 0.2644 0.184943 0.268051 0.229503 0.1979 0.20035 0.164427 0.203089 0.119084 0.142398 0.185551
F3D9 0.153265 0.186706 0.196143 0.21504 0.20728 0.262127 0.255558 0.174563 0.2607 0.221969 0.192437 0.185154 0.13976 0.195538 0.0973901 0.127619 0.177605 0.0558726
Mock 0.653789 0.645344 0.633297 0.623553 0.633903 0.633135 0.63394 0.635815 0.645332 0.636453 0.629143 0.646918 0.663222 0.639517 0.649722 0.64073 0.654882 0.63988 0.646155
As this distance matrix come from a PCoA, what I want to do is to plot these distances in an ordination plot with R.
Any idea on how to doing this?
Thanks a lot
You have the vegan library with metaMDS function that generates coordinates for each sample using such a distance matrix as the input.
Let's call M to your matrix, you need to run this code:
# Load the library
library(vegan)
# Use metaMDS function for 2D - plot
NMDS <- metaMDS(distance = M, k = 2)
# Plot your individuals
plot(NMDS$points[,1], NMDS$points[,2])
In NMDS$points you have the coordinates for each of the samples. I suggest to colour the individuals according to a factor of interest such as cases and controls for example in biomedical analyses.
Thanks to #R18, finally I could manage with this issue.
For the distance table I uploaded, the solution that I reached was to use the following code:
library(phyloseq)
library(vegan)
M <- import_mothur_dist("pcoa_UFdistance_matrix.dist")
unifrac <- metaMDS(M, distance = M, k = 2, trymax=100)
plot(unifrac$points[,1], unifrac$points[,2], main="Principal Coordinates Analysis", col.main="red", font.main=4, xlab="PCoA 1", ylab="PCoA 2")
text(unifrac, pos=3)
Hope it will help someone!!
I am trying to plot GIS coordinates, specifically UK national Grid Coordinates which eastings and northings ressemble:
194630000 562220000
I can plot these using clusplot in the Cluster library:
clusplot (df2,k.means.fit$cluster,main=i,color=TRUE,shade=FALSE,labels=0,lines=0,bty="7")
where df2 is my data frame and k.means.fit is the result of the K means analysis on df2.
Note that the coordinates of the centers after the k means analysis have not been normalised:
k.means.fit$centers
# Grid.Ref.Northing Grid.Ref.Easting
#1 206228234 581240726
But when I plot the clusters, all the points are translated such that they are centered around the origin.
I am wanting to show a map in the backround for context of the plots, but unless I am able to stop the translation, or at least know the values the function used, I cannot allign these properly.
I understand clusplots is designed to do a lot of feature automatically, which limits customisation, but I am not able to find a package that creates similar cluster plots.
Intended plot
(this was done at a random placement and is innaccurate)
Actual cluster diagram
Here is a way to produce something close what you are asking for.
Because of the need to translate between (lat, lon) and the graphics
coordinates (x,y) I did not use clusplot. Instead, I am using RgoogleMaps to get the background map and do the coordinate translations. I use car to plot the ellipses.
library(RgoogleMaps)
library(car)
## Some setup to get the map of the Chelmsford area.
lat <- c(51.7,51.8)
lon <- c(0.4, 0.5)
center = c(mean(lat), mean(lon))
zoom <- 10
Chelmsford <- GetMap(center=center, zoom=zoom, maptype= "roadmap",
destfile = "Chelsford.png")
You did not provide any points to test on, so I made up a few. I realize that my points are more separable than yours, but that only affects the clustering algorithm, not the mapping.
## Some Test Data
MC = structure(c(51.7965309028563, 51.794104389723, 51.7908688357699,
51.7787334409852, 51.7633572542762, 51.7674041270742, 51.7479758289189,
51.7649760469292, 51.7447369665147, 51.7576910228736, 51.7487855082363,
51.7601194948316, 51.754452857092, 51.7309692105151, 51.7107148897781,
51.6977473627376, 51.7139561908073, 51.7366387945275, 51.7325891642372,
51.7050420540348, 51.7050420540348, 51.7285391710661, 51.6677457194661,
51.6571998818184, 51.6466515895592, 51.6377241941241, 51.6377241941241,
51.645028557487, 51.6636899185361, 51.6580111872422, 51.6385358481586,
51.63528914486, 51.8789546795942, 51.8571513038925, 51.8531124817854,
51.8514968514399, 51.8676505449041, 51.8805693240155, 51.862805045846,
51.8506890145161, 51.8345292307446, 51.8337210892835, 51.8256388769982,
51.812704320496, 51.8232139304917, 51.8312965778826, 51.8240222604979,
51.8135128390641, 51.8094701011681, 51.807044284361, 51.7973397115523,
51.7803516822409, 51.7803516822409, 51.7949132419417, 51.7949132419417,
51.7811607811046, 51.7763059702794, 51.7787334409852, 51.9007474867743,
51.8781473356377, 51.8910630993239, 51.8757252167833, 51.8821839104485,
51.8821839104485, 51.8595744231562, 51.8821839104485, 51.8741103983922,
51.8660354365472, 51.8797620090535, 51.8765326042323, 51.8652278606205,
51.8934843918728, 51.8829911819196, 0.0895846775599907, 0.109172466823018,
0.153571455819268, 0.144430487496514, 0.140512929643877, 0.115701729910693,
0.109172466823018, 0.0882788249424316, 0.124842698233447, 0.171853392464776,
0.423882947649248, 0.447388294764912, 0.477422904968252, 0.45130585261751,
0.442164884294756, 0.468281936645498, 0.502234104701436, 0.504845809936514,
0.487869725908525, 0.430412210736963, 0.399071747916064, 0.395154190063467,
0.520516041346943, 0.527045304434619, 0.523127746582022, 0.511375073024189,
0.517904336111865, 0.54010383061001, 0.550550651550283, 0.55577406202044,
0.572750146048389, 0.508763367789111, 0.513986778259268, 0.504845809936514,
0.515292630876787, 0.537492125374932, 0.549244798932764, 0.588420377458818,
0.587114524841299, 0.550550651550283, 0.508763367789111, 0.493093136378682,
0.515292630876787, 0.485258020673487, 0.508763367789111, 0.504845809936514,
0.652407155718095, 0.669383239746084, 0.668077387128565, 0.644572040012901,
0.640654482160303, 0.640654482160303, 0.643266187395342, 0.606702314104326,
0.608008166721885, 0.619760840279717, 0.626290103367393, 0.594949640546534,
0.162712424142022, 0.156183161054346, 0.194052886962881, 0.182300213405049,
0.212334823608389, 0.217558234078545, 0.220169939313624, 0.238451875959131,
0.25542795998708, 0.259345517839678, 0.27109819139751, 0.28546257019042,
0.284156717572901, 0.295909391130693, 0.30113280160085), .Dim = c(73L,
2L), .Dimnames = list(NULL, c("lat", "lon")))
Plot the map and points just to get oriented.
PlotOnStaticMap(Chelmsford)
P1 = LatLon2XY.centered(Chelmsford, MC[,1], MC[,2], 10)
names(P1) = c("x", "y")
points(P1, pch=16)
Now we need to find and plot the clusters.
set.seed(42) ## For reproducibility
Clust = kmeans(MC, 7)
## Convert to graphics coordinates
Points = LatLon2XY.centered(Chelmsford, MC[,1], MC[,2], 10)
names(Points) = c("x", "y")
Points = data.frame(Points)
## Replot noting clusters
PlotOnStaticMap(Chelmsford)
points(Points, pch=21, bg=Clust$cluster)
## Add ellipses
for(i in 1:length(unique(Clust$cluster))) {
dataEllipse(Points[Clust$cluster == i,1], Points[Clust$cluster == i,2],
center.pch=10, levels=0.90, fill=TRUE, fill.alpha=0.1,
plot.points=FALSE, col=i, lwd=1,)
}
Et voila!
I have used smoothing to create two "functions" fd4 and fd6.
fit6 <- smooth.basis(tid6, zbegfor, fdParobj2)
fd6 <- fit6$fd
I want to measure the L2 distance between them on the interval [0,1], but I haven't been able to find an appropriate way.
||f − g||_2 = sqrt(int(|f(x)-g(x)|^2,0,1))
The best bet has been this one: How to calculate functional L_2 norm using R, but when I use fd6 instead of f <- function(x) x^2, I get the following message:
"Error in fac - fdmat : non-conformable arrays".
I've spent hours trying to find a solution. Please help me!
Now with reproducible code:
library(fda)
# Smoothing of movement pattern without obstacle rescaled to the interval [0,1]
without <- c(22.5050173512478, 22.5038665040295, 22.5171851824298, 22.5368096190746,
22.5770229184757, 22.6709727229898, 22.8195669635573, 23.0285400460222,
23.3240853426905, 23.6895323912605, 24.0905709304813, 24.5674870961964,
25.129085512519, 25.7433521858875, 26.4096817521118, 27.1338935155912,
27.906416101033, 28.7207273157549, 29.5431756517467, 30.3697951466496,
31.2214907341765, 32.0625307132683, 32.8786845916855, 33.671550678219,
34.4449992914392, 35.1852293010227, 35.8866367048324, 36.5650863548079,
37.1776116180247, 37.7706354957587, 38.3082855431959, 38.8044130844639,
39.2471137254193, 39.6193031585418, 39.9685683244076, 40.2345560551869,
40.4394442661545, 40.5712407258558, 40.6905311089523, 40.712419802203,
40.6704560575084, 40.5583379372846, 40.3965425630546, 40.1443139907057,
39.8421899334408, 39.4671160834355, 39.018733225651, 38.5381390971577,
38.035680135599, 37.4625783280288, 36.8649362406917, 36.2320264206665,
35.5599736527209, 34.8983871226943, 34.2058073957721, 33.4893682831911,
32.7568501019309, 32.0241649500974, 31.3036406455137, 30.587636320768,
29.8962657607091, 29.2297665999702, 28.6003939337949, 28.0003531206639,
27.433551463149, 26.9088532545635, 26.4265682839796, 25.974193299003,
25.5553146923473, 25.1701249455904, 24.8107813804098, 24.4776168601955,
24.167582682288, 23.8726502760669, 23.589703789663, 23.3222235336882,
23.0616248799115, 22.8185342685607, 22.6767541125512, 22.6567795841271,
22.6488510112824, 22.6436058079441, 22.6391304188382)
timewithout <- (1:length(without))/length(without) # For scaling
splineBasis = create.bspline.basis(c(0,1), nbasis=25, norder=6) # The basis for smoothing
basis = fdPar(fdobj=splineBasis, Lfdobj=2, lambda=0.00001)
fitwithout <- smooth.basis(timewithout, without, basis) # Smoothing
fdwithout <- fitwithout$fd
# Same but movement is over an obstacle
with <- c(22.4731637093167, 22.4655561889073, 22.4853719755102, 22.4989400065304,
22.5495656349031, 22.666945409755, 22.8368941117498, 23.0846080078369,
23.4160560011242, 23.8285634914224, 24.2923085321078, 24.8297004047422,
25.4884540279408, 26.2107053559, 27.0614232848574, 27.9078055119721,
28.8449720096674, 29.8989669834473, 30.996962022701, 32.1343108758062,
33.3286403418359, 34.6364870430171, 35.9105342483246, 37.1883582665643,
38.467212668323, 39.7381525466373, 41.0395064969214, 42.3095531191294,
43.5708069740233, 44.7881178787717, 45.9965529977777, 47.1643807808923,
48.284786275036, 49.3593991064962, 50.3863035442644, 51.3535489662494,
52.2739716491521, 53.1338828493223, 53.9521101656512, 54.7037562884229,
55.3593092084143, 55.9567618011946, 56.4768579145271, 56.9251919073806,
57.2971965985674, 57.5937987523734, 57.8158626068961, 57.9554856023804,
58.009777126789, 57.9863251605612, 57.8932199088797, 57.6988126618694,
57.4350394069443, 57.1112025796509, 56.7580579506751, 56.2680669960935,
55.6963799946038, 55.0574070566765, 54.3592140352073, 53.6072275005723,
52.7876353306759, 51.9172334605074, 50.9879178368431, 49.9953932631072,
48.9460707853802, 47.8511977258834, 46.6827266395278, 45.4635999409637,
44.2633368255294, 43.0386729762103, 41.7880095105045, 40.4834298069985,
39.1610223705633, 37.9241872458281, 36.7158342529737, 35.5408830466013,
34.4070964101159, 33.307156473109, 32.2514661493348, 31.2475129673168,
30.2990631096187, 29.4096423238141, 28.590173995037, 27.8437368908309,
27.17493959411, 26.5779670740351, 26.0377946174036, 25.5731202027558,
25.1761397934058, 24.8319659155494, 24.5479180062239, 24.2940808334792,
24.09388897537, 23.934861348149, 23.7999923744404, 23.6877461628934,
23.5982309560843, 23.5207597985246, 23.4354446383638, 23.3604065265148,
23.2819126915765, 23.1725048152396, 23.0637455648184, 22.9426779696074,
22.8079176617495, 22.69360227086, 22.6622165457034, 22.6671302753094,
22.66828206305, 22.6703162730529, 22.6715781657376)
timewith <- (1:length(with))/length(with)
fitwith <- smooth.basis(timewith, with, basis) # Smoothing
fdwith <- fitwith$fd
# Plots for understanding
plot(fdwith, col=2) # Smoothed curve for movement over obstacle
plot(fdwithout, col=2, add = TRUE) # Same but no obstacle
# I have to find the L2-distance between these curves
First, one can take advantage of the possibility to perform arithmetic operations with fd objects: fdwith - fdwithout. Second, maybe there is a better way to extract values from fd objects at specific points, but this also works: predict(newdata = 0.5, fdwith - fdwithout). So,
sqrt(integrate(function(x) predict(newdata = x, fdwith-fdwithout)^2, lower = 0, upper = 1)$val)
# [1] 9.592434