I have a dataframe including 520 individual identification #s and their frequency count over 10 bin/indices. I want to fit nls() for each of them & save their coef and r^2 values each in a new column.
structure(list(User.ID = c("37593", "38643", "49433", "60403",
"70923"), V1 = c(9L, 3L, 4L, 80L, 19L), V2 = c(10L, 0L, 29L,
113L, 21L), V3 = c(5L, 2L, 17L, 77L, 7L), V4 = c(2L, 2L, 16L,
47L, 4L), V5 = c(2L, 10L, 16L, 40L, 1L), V6 = c(4L, 0L, 9L, 22L,
1L), V7 = c(6L, 8L, 9L, 8L, 0L), V8 = c(2L, 17L, 16L, 24L, 2L
), V9 = c(3L, 20L, 7L, 30L, 0L), V10 = c(2L, 11L, 5L, 11L, 2L
)), row.names = c(NA, 5L), class = "data.frame")
I like to perform nls() or fit exponential for each row, meaning each user. I also want to add three columns to the existing dataframe for A, k, and r^2 each to record their values for each user/each row.
What I have tried:
FreqBin=c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
for (i in 1:nrow(Mfrq.df.2)){
eaUser=Mfrq.df.2[i,2:11]
ExpFit[i]=nls(eaUser[i]~A*exp(k*FreqBin), start=c(A=1, k=0.01))
}
This code is what I tried, but I got this error.
Error in model.frame.default(formula = ~eaUser + i + FreqBin) :
invalid type (list) for variable 'eaUser'
Any code suggestion that I should try?