Вы можете попытаться создать формулу в функции, используя определенные вами переменные y и x:
standard.lm= function(standards,date_field = "Date",
peak_field,std_field,peak_field2 = NA){
lm_form = as.formula(paste(std_field,"~",peak_field))
#another away
#lm_form = substitute(y~x,list(y=as.name(std_field),x=as.name(peak_field)))
y = standards %>% nest(data=-one_of(date_field)) %>%
mutate(fit = map(data, ~lm(lm_form, data = .)))
return(y) }
Мы проверяем это:
standard_data = structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("10/18/2019",
"10/24/2019"), class = "factor"), ppm = c(0, 0, 0.1, 0.1, 0.1,
1, 1, 1, 0, 0, 0, 0, 0.1, 0.1, 0.1, 0.1, 1, 1, 1), area = c(0,
0, 438.9804, 447.1784, 443.7794, 3232.2088, 3206.6672, 3206.232,
0, 15.98, 0, 0, 379.387, 325.5268, 325.8126, 310.5972, 3259.366,
3218.0836, 3192.7076)), class = "data.frame", row.names = c(NA,
-19L))
mdl = standard.lm(standard_data, std_field = "ppm", peak_field = "area")
predict(mdl$fit[[1]], data.frame(area=c(1300.1,1400.3)))
1 2
0.3897161 0.4215205