简体   繁体   中英

Maximum first derivative in for values in a data frame R

Good day, I am looking for some help in processing my dataset. I have 14000 rows and 500 columns and I am trying to get the maximum value of the first derivative for individual rows in different column groups. I have my data saved as a data frame with the first column being the name of a variable. My data looks like this:

 Species   Spec400   Spec405   Spec410   Spec415
1  AfricanOilPalm_1_Lf_1 0.2400900 0.2318345 0.2329633 0.2432734
2 AfricanOilPalm_1_Lf_10 0.1783162 0.1808581 0.1844433 0.1960315
3 AfricanOilPalm_1_Lf_11 0.1699646 0.1722618 0.1615062 0.1766804
4 AfricanOilPalm_1_Lf_12 0.1685733 0.1743336 0.1669799 0.1818896
5 AfricanOilPalm_1_Lf_13 0.1747400 0.1772355 0.1735916 0.1800227

For each of the variables in the species column, I want to get the maximum derivative from Spec495 to Spec500 for example. This is what I did before I ran into errors.

x<-c(495,500,505,510,515,520,525,530,535,540,545,550)##get x values of     reflectance(Spec495 to Spec500)

y.data.f<-hsp[,21:32]##get row values for the required columns

y<-as.numeric(y.data.f[1,])##convert to a vector, for just the first row of data

library(pspline) ##Using a spline so a derivative maybe calculated from a list of   numeric values

I really wanted to avoid using a loop because of the time it takes, but this is the only way I know of thus far

for(j in 1:14900)
+ { y<-as.numeric(y.data.f[j,]) + a1d<-max(predict(sm.spline(x, y), x, 1))
+     write.table(a1d, file = "a1-d-appended.csv", sep = ",", 
+ col.names = FALSE,   append=TRUE) + }

This loop runs up until the 7861th value then get this error:

Error in smooth.Pspline(x = ux, y = tmp[, 1], w = tmp[, 2], method = method,  : 
NA/NaN/Inf in foreign function call (arg 6)

I am sure there must be a way to avoid using a loop, maybe using the plyr package, but I can't figure out how to do so, nor which package would be best to get the value for maximum derivative.

Can anyone offer some insight or suggestions? Thanks in advance

First differences are the numerical analog of first derivatives when the x-dimension is evenly spaced. So something along the lines of:

 which.max( diff ( predict(sm.spline(x, y))$ysmth) ) )

... will return the location of the maximum (positive) slope of the smoothed spline. If you wanted the maximal slope allowing it to be either negative or postive you would use abs() around the predict()$ysmth. If you are having difficulties with non-finite values then using an index of is.finite will clear both Inf and NaN difficulties:

predy <- predict(sm.spline(x, y))$ysmth
predx <- predict(sm.spline(x, y))$x
is.na( predy ) <- !is.finite(pred)
plot(predx, predy,  # NA values will not blow up R plotting function,
                   # ...  just create discontinuities.
                  main ="First Derivative")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM