How do I apply optim() to custom function to each row and bind some of the output as well as function-calculated variables to correct row in existing df ?

There is a very similar topic here Apply function to each row in a DF and create a new DF with the outputs, and I was able to get part of what I want working. Thank you to everyone who replied in that topic! Very helpful!

Question 1: How do I rbind only the result$par to existing dataframe lm_df5, because this contains the two estimated parameters of interest (b0:b1)? In practice, I will be estimating many more parameters in my model/function.

Question 2: Additionally, how do I rbind the sse and sse_sqrt variables calculated within function lm_sse5 for each row to the same existing df lm_df5? In practice, I will be calculating a lot of model fit indices that I want to add to the existing dataframe lm_df5.

My problem space is this. The first part is that I want to apply() optim() to a custom function lm_sse5 to each row. My custom function contains estimated parameters (which will be output and bound if using previous topic's advice--thank you!) and also newly calculated variables (which are NOT bound into the same dataframe). So the second part is that I want the estimated parameters AND the variables (e.g., model fit indices) calculated from within the function lm_sse5 to also be bound to the correct row on an existing lm_df5. The existing data frame may contain other demographic and experiment information (e.g., IV levels, age, etc.), so it's critical that all the data for a row are correctly bound together.

As I said, following the topic linked in the first paragraph, I was able to properly lapply() and rbind() similar to how I want. This next line of code binds ALL the values of result_list (incl. result$par, result$value, etc.). The caveat here that I just want result$par. Here is that code:

EDIT: I followed the example in the FAQ and used reprex function to paste the code below.

# Now lets create a dataframe with 2 participants 
# In practice, this df will contain experiment and demographic info
lm_df5<-data.frame(H1=c(1,5),H2=c(4,7),H3=c(12,17),H4=c(21,32),H5=c(25,41),H6=c(52,64),
                   F1=c(2,8),F2=c(9,10),F3=c(18,21),F4=c(21,32),F5=c(31,49),F6=c(59,68)) 
# Create optim() function to minimize sse for a line for one subject at a time
lm_sse5<-function(dat,par){
  b0<-par[1]
  b1<-par[2]
  
  # Convert wide to long inside of function
  lm_df5<-data.frame(x=c(dat[["H1"]],dat[["H2"]],dat[["H3"]],dat[["H4"]],dat[["H5"]],dat[["H6"]]),
                     y=c(dat[["F1"]],dat[["F2"]],dat[["F3"]],dat[["F4"]],dat[["F5"]],dat[["F6"]]))
  
  yhat<-b0 + b1*lm_df5[["x"]]
  sse<- sum((yhat-lm_df5[["y"]])**2)
  sse_sqrt<-sqrt(sse)
  
  # How to get sse and sse_sqrt into dataframe?
  #  This seemed to work but seems tedious.  This replaces the dataframe with a new value for each iteration
  # but because it's being overwritten each time, I end up with the last value in the output dataframe.
  output<<-data.frame("sse"="","sse_sqrt"="")
  output[["sse"]]<<-sse
  output[["sse_sqrt"]]<<-sse_sqrt
  
  print(paste0("sse is: ",sse))
  print(paste0("sse_sqrt is: ", sse_sqrt))
  
  return(sse)
}

# Define parameter start values
param_start<-c(b0=0,b1=0)

# Run minimization function on return value in function, which is sse

# try to use apply function
# https://forum.posit.co/t/apply-function-to-each-row-in-a-df-and-create-a-new-df-with-the-outputs/38946/7?u=dbzgtfan4ever
result_list<-do.call(rbind,sapply(1:nrow(lm_df5),function(x) optim(param_start,
                                                                fn=lm_sse5,
                                                                dat=lm_df5[x,])))
#> [1] "sse is: 5292"
#> [1] "sse_sqrt is: 72.7461339178928"
#> [1] "sse is: 5264.06"
#> [1] "sse_sqrt is: 72.5538420760748"
#> [1] "sse is: 4423.71"
#> [1] "sse_sqrt is: 66.5109765377114"
#> [1] "sse is: 4398.07"
#> [1] "sse_sqrt is: 66.3179462890702"
#> [1] "sse is: 3982.3575"
#> [1] "sse_sqrt is: 63.1059228599028"
#> [1] "sse is: 3257.5775"
#> [1] "sse_sqrt is: 57.0751916334934"
#> [1] "sse is: 2436.456875"
#> [1] "sse_sqrt is: 49.3604788773367"
#> [1] "sse is: 2113.026875"
#> [1] "sse_sqrt is: 45.9676720641801"
#> [1] "sse is: 1279.04296875"
#> [1] "sse_sqrt is: 35.7637102206972"
#> [1] "sse is: 500.13296875"
#> [1] "sse_sqrt is: 22.3636528489869"
#> [1] "sse is: 51.5982421875"
#> [1] "sse_sqrt is: 7.18319164351752"
#> [1] "sse is: 226.004492187501"
#> [1] "sse_sqrt is: 15.0334457855643"
#> [1] "sse is: 54.2566088867187"
#> [1] "sse_sqrt is: 7.36590855812904"
#> [1] "sse is: 1102.69385498047"
#> [1] "sse_sqrt is: 33.2068344619066"
#> [1] "sse is: 292.720452117921"
#> [1] "sse_sqrt is: 17.1090751391746"
#> [1] "sse is: 380.895009002685"
#> [1] "sse_sqrt is: 19.5165316847714"
#> [1] "sse is: 101.248921012879"
#> [1] "sse_sqrt is: 10.0622522833051"
#> [1] "sse is: 145.336199455261"
#> [1] "sse_sqrt is: 12.0555464187759"
#> [1] "sse is: 58.8919480419161"
#> [1] "sse_sqrt is: 7.67410894123325"
#> [1] "sse is: 80.9355872631072"
#> [1] "sse_sqrt is: 8.99642080291419"
#> [1] "sse is: 51.0581597018242"
#> [1] "sse_sqrt is: 7.14549926190075"
#> [1] "sse is: 59.1341577792168"
#> [1] "sse_sqrt is: 7.68987371672752"
#> [1] "sse is: 51.169206213206"
#> [1] "sse_sqrt is: 7.15326542309217"
#> [1] "sse is: 49.2387046007812"
#> [1] "sse_sqrt is: 7.01702961378824"
#> [1] "sse is: 48.4156210802123"
#> [1] "sse_sqrt is: 6.95813344800258"
#> [1] "sse is: 53.8322604538128"
#> [1] "sse_sqrt is: 7.33704712086632"
#> [1] "sse is: 49.7332029319624"
#> [1] "sse_sqrt is: 7.05217717672794"
#> [1] "sse is: 47.6602656771499"
#> [1] "sse_sqrt is: 6.9036414794766"
#> [1] "sse is: 47.2142158163916"
#> [1] "sse_sqrt is: 6.87126013307542"
#> [1] "sse is: 44.8423099657771"
#> [1] "sse_sqrt is: 6.69644009648239"
#> [1] "sse is: 42.7625174615758"
#> [1] "sse_sqrt is: 6.53930557946146"
#> [1] "sse is: 45.1157169014189"
#> [1] "sse_sqrt is: 6.71682342342114"
#> [1] "sse is: 44.4844581327662"
#> [1] "sse_sqrt is: 6.66966701813263"
#> [1] "sse is: 40.1999398893486"
#> [1] "sse_sqrt is: 6.34034225332896"
#> [1] "sse is: 37.614712402919"
#> [1] "sse_sqrt is: 6.13308343355273"
#> [1] "sse is: 37.2930628177838"
#> [1] "sse_sqrt is: 6.10680463235756"
#> [1] "sse is: 36.6979583416925"
#> [1] "sse_sqrt is: 6.05788398219152"
#> [1] "sse is: 34.3719466777763"
#> [1] "sse_sqrt is: 5.86275930580271"
#> [1] "sse is: 37.0268894505976"
#> [1] "sse_sqrt is: 6.08497242808853"
#> [1] "sse is: 40.856670065405"
#> [1] "sse_sqrt is: 6.39192225120151"
#> [1] "sse is: 35.4246086371405"
#> [1] "sse_sqrt is: 5.951857578701"
#> [1] "sse is: 33.4880133804883"
#> [1] "sse_sqrt is: 5.78688287253927"
#> [1] "sse is: 33.7464000676461"
#> [1] "sse_sqrt is: 5.80916517820298"
#> [1] "sse is: 34.4463524867059"
#> [1] "sse_sqrt is: 5.86910150591263"
#> [1] "sse is: 33.7690060472227"
#> [1] "sse_sqrt is: 5.8111105691789"
#> [1] "sse is: 33.5272078268965"
#> [1] "sse_sqrt is: 5.79026837261422"
#> [1] "sse is: 33.3743338362787"
#> [1] "sse_sqrt is: 5.77705234841079"
#> [1] "sse is: 33.9958155096619"
#> [1] "sse_sqrt is: 5.83059306671816"
#> [1] "sse is: 33.456690298608"
#> [1] "sse_sqrt is: 5.78417585301554"
#> [1] "sse is: 33.6307238126984"
#> [1] "sse_sqrt is: 5.79920027354621"
#> [1] "sse is: 33.3967653812446"
#> [1] "sse_sqrt is: 5.77899345744955"
#> [1] "sse is: 33.4473832025728"
#> [1] "sse_sqrt is: 5.78337126618833"
#> [1] "sse is: 33.3901032010217"
#> [1] "sse_sqrt is: 5.77841701515403"
#> [1] "sse is: 33.4286720877091"
#> [1] "sse_sqrt is: 5.78175337486035"
#> [1] "sse is: 33.3765541985944"
#> [1] "sse_sqrt is: 5.77724451608156"
#> [1] "sse is: 33.381832393177"
#> [1] "sse_sqrt is: 5.77770130702315"
#> [1] "sse is: 33.3722342048265"
#> [1] "sse_sqrt is: 5.77687062386086"
#> [1] "sse is: 33.3978859304671"
#> [1] "sse_sqrt is: 5.7790904068432"
#> [1] "sse is: 33.370489197007"
#> [1] "sse_sqrt is: 5.77671958788091"
#> [1] "sse is: 33.3770988887798"
#> [1] "sse_sqrt is: 5.77729165689078"
#> [1] "sse is: 33.3711538051582"
#> [1] "sse_sqrt is: 5.77677711229698"
#> [1] "sse is: 33.3717130722072"
#> [1] "sse_sqrt is: 5.7768255185878"
#> [1] "sse is: 33.3704993486376"
#> [1] "sse_sqrt is: 5.77672046654827"
#> [1] "sse is: 33.3731906023095"
#> [1] "sse_sqrt is: 5.77695340143137"
#> [1] "sse is: 33.3702752401515"
#> [1] "sse_sqrt is: 5.77670106896241"
#> [1] "sse is: 33.3704180621931"
#> [1] "sse_sqrt is: 5.77671343085262"
#> [1] "sse is: 33.3702116376073"
#> [1] "sse_sqrt is: 5.77669556386757"
#> [1] "sse is: 33.3707395302216"
#> [1] "sse_sqrt is: 5.77674125525989"
#> [1] "sse is: 33.3702019277075"
#> [1] "sse_sqrt is: 5.77669472343031"
#> [1] "sse is: 8654"
#> [1] "sse_sqrt is: 93.0268778364619"
#> [1] "sse is: 8616.46"
#> [1] "sse_sqrt is: 92.8248889037849"
#> [1] "sse is: 7155.24"
#> [1] "sse_sqrt is: 84.5886517211381"
#> [1] "sse is: 7121.02"
#> [1] "sse_sqrt is: 84.3861363021201"
#> [1] "sse is: 6410.795"
#> [1] "sse_sqrt is: 80.0674403237671"
#> [1] "sse is: 5161.115"
#> [1] "sse_sqrt is: 71.840900606827"
#> [1] "sse is: 3766.15375"
#> [1] "sse_sqrt is: 61.3689966514037"
#> [1] "sse is: 3232.87375"
#> [1] "sse_sqrt is: 56.858365699341"
#> [1] "sse is: 1853.5134375"
#> [1] "sse_sqrt is: 43.0524498431854"
#> [1] "sse is: 609.363437499999"
#> [1] "sse_sqrt is: 24.6852878755748"
#> [1] "sse is: 64.8296093750001"
#> [1] "sse_sqrt is: 8.05168363604781"
#> [1] "sse is: 561.217109375002"
#> [1] "sse_sqrt is: 23.6900213038106"
#> [1] "sse is: 47.9707568359377"
#> [1] "sse_sqrt is: 6.92609246515939"
#> [1] "sse is: 2411.96806152344"
#> [1] "sse_sqrt is: 49.1117914713304"
#> [1] "sse is: 434.709180603027"
#> [1] "sse_sqrt is: 20.849680587554"
#> [1] "sse is: 713.936492614748"
#> [1] "sse_sqrt is: 26.7195900532689"
#> [1] "sse is: 114.911530380249"
#> [1] "sse_sqrt is: 10.7196795838425"
#> [1] "sse is: 254.525186386109"
#> [1] "sse_sqrt is: 15.9538455046459"
#> [1] "sse is: 52.4138248252868"
#> [1] "sse_sqrt is: 7.23973927881984"
#> [1] "sse is: 70.0215524864194"
#> [1] "sse_sqrt is: 8.36788817363254"
#> [1] "sse is: 49.474433425665"
#> [1] "sse_sqrt is: 7.03380646774312"
#> [1] "sse is: 85.8902722299104"
#> [1] "sse_sqrt is: 9.26770048231547"
#> [1] "sse is: 45.4544009187072"
#> [1] "sse_sqrt is: 6.74198790555925"
#> [1] "sse is: 44.5480412622541"
#> [1] "sse_sqrt is: 6.67443190558223"
#> [1] "sse is: 46.6082978003284"
#> [1] "sse_sqrt is: 6.8270270103705"
#> [1] "sse is: 52.2335206231472"
#> [1] "sse_sqrt is: 7.22727615517404"
#> [1] "sse is: 45.2043138433062"
#> [1] "sse_sqrt is: 6.72341534068112"
#> [1] "sse is: 44.0508614013158"
#> [1] "sse_sqrt is: 6.63708229580708"
#> [1] "sse is: 44.0484828741616"
#> [1] "sse_sqrt is: 6.63690310869171"
#> [1] "sse is: 42.5971971339872"
#> [1] "sse_sqrt is: 6.52665282775077"
#> [1] "sse is: 41.9011630142422"
#> [1] "sse_sqrt is: 6.4731107679571"
#> [1] "sse is: 41.3049253497775"
#> [1] "sse_sqrt is: 6.426890799584"
#> [1] "sse is: 41.5321604227333"
#> [1] "sse_sqrt is: 6.44454501285648"
#> [1] "sse is: 39.5776408005644"
#> [1] "sse_sqrt is: 6.29107628316208"
#> [1] "sse is: 39.7723167034236"
#> [1] "sse_sqrt is: 6.30652968782544"
#> [1] "sse is: 38.8847238597276"
#> [1] "sse_sqrt is: 6.23576169042143"
#> [1] "sse is: 39.2252973116644"
#> [1] "sse_sqrt is: 6.26301024361803"
#> [1] "sse is: 37.577474621221"
#> [1] "sse_sqrt is: 6.1300468694147"
#> [1] "sse is: 38.1438461966005"
#> [1] "sse_sqrt is: 6.17607044945251"
#> [1] "sse is: 36.7878784040119"
#> [1] "sse_sqrt is: 6.06530117999196"
#> [1] "sse is: 37.2417902349297"
#> [1] "sse_sqrt is: 6.10260520064421"
#> [1] "sse is: 35.9006644762119"
#> [1] "sse_sqrt is: 5.99171632140674"
#> [1] "sse is: 36.8387317241118"
#> [1] "sse_sqrt is: 6.06949188351972"
#> [1] "sse is: 35.0143889826305"
#> [1] "sse_sqrt is: 5.9172957491265"
#> [1] "sse is: 35.5816391925294"
#> [1] "sse_sqrt is: 5.96503471846807"
#> [1] "sse is: 34.5472103655371"
#> [1] "sse_sqrt is: 5.87768750152108"
#> [1] "sse is: 35.8569732859574"
#> [1] "sse_sqrt is: 5.98806924525405"
#> [1] "sse is: 33.5642555955835"
#> [1] "sse_sqrt is: 5.79346663022956"
#> [1] "sse is: 34.2448441844633"
#> [1] "sse_sqrt is: 5.85190944773271"
#> [1] "sse is: 33.5171122891964"
#> [1] "sse_sqrt is: 5.78939653929461"
#> [1] "sse is: 35.1985708821371"
#> [1] "sse_sqrt is: 5.93283834957073"
#> [1] "sse is: 32.4374782428706"
#> [1] "sse_sqrt is: 5.69539096488297"
#> [1] "sse is: 33.2314052107316"
#> [1] "sse_sqrt is: 5.76466869913021"
#> [1] "sse is: 32.8103702471902"
#> [1] "sse_sqrt is: 5.7280337156122"
#> [1] "sse is: 32.3913173493741"
#> [1] "sse_sqrt is: 5.69133704408499"
#> [1] "sse is: 32.463372980594"
#> [1] "sse_sqrt is: 5.69766381779357"
#> [1] "sse is: 32.0274165762031"
#> [1] "sse_sqrt is: 5.65927703653065"
#> [1] "sse is: 32.2971027097103"
#> [1] "sse_sqrt is: 5.68305399496699"
#> [1] "sse is: 31.9990219628378"
#> [1] "sse_sqrt is: 5.65676780174313"
#> [1] "sse is: 32.084193450328"
#> [1] "sse_sqrt is: 5.66429108100281"
#> [1] "sse is: 31.8528004411163"
#> [1] "sse_sqrt is: 5.64382852690586"
#> [1] "sse is: 32.0218797410245"
#> [1] "sse_sqrt is: 5.65878783318693"
#> [1] "sse is: 31.8436346230437"
#> [1] "sse_sqrt is: 5.64301644717111"
#> [1] "sse is: 31.8953128724739"
#> [1] "sse_sqrt is: 5.64759354703169"
#> [1] "sse is: 31.7751715445316"
#> [1] "sse_sqrt is: 5.63694700565223"
#> [1] "sse is: 31.8767298532573"
#> [1] "sse_sqrt is: 5.6459480916191"
#> [1] "sse is: 31.7646202061926"
#> [1] "sse_sqrt is: 5.63601101899141"
#> [1] "sse is: 32.1644041659143"
#> [1] "sse_sqrt is: 5.67136704559971"
#> [1] "sse is: 31.7467665872035"
#> [1] "sse_sqrt is: 5.63442690849775"
#> [1] "sse is: 31.7867580311857"
#> [1] "sse_sqrt is: 5.63797463910452"
#> [1] "sse is: 31.7516858956548"
#> [1] "sse_sqrt is: 5.63486343185483"
#> [1] "sse is: 31.7361989964657"
#> [1] "sse_sqrt is: 5.63348906065022"
#> [1] "sse is: 31.7535327005221"
#> [1] "sse_sqrt is: 5.63502730255338"
#> [1] "sse is: 31.8081527246932"
#> [1] "sse_sqrt is: 5.63987169399209"
#> [1] "sse is: 31.7369616240624"
#> [1] "sse_sqrt is: 5.6335567472124"
#> [1] "sse is: 31.7253193942049"
#> [1] "sse_sqrt is: 5.63252335940162"
#> [1] "sse is: 31.7206378437017"
#> [1] "sse_sqrt is: 5.63210776208177"
#> [1] "sse is: 31.7121090768581"
#> [1] "sse_sqrt is: 5.63135055531603"
#> [1] "sse is: 31.702728446106"
#> [1] "sse_sqrt is: 5.63051760019503"
#> [1] "sse is: 31.68959306346"
#> [1] "sse_sqrt is: 5.62935103395232"
#> [1] "sse is: 31.6830858699802"
#> [1] "sse_sqrt is: 5.62877303415053"
#> [1] "sse is: 31.6442839414952"
#> [1] "sse_sqrt is: 5.62532522984185"
#> [1] "sse is: 31.6092665045491"
#> [1] "sse_sqrt is: 5.62221188719788"
#> [1] "sse is: 31.6156608134304"
#> [1] "sse_sqrt is: 5.62278052332033"
#> [1] "sse is: 31.6228465359347"
#> [1] "sse_sqrt is: 5.62341947003198"
#> [1] "sse is: 31.5272679454244"
#> [1] "sse_sqrt is: 5.61491477632781"
#> [1] "sse is: 31.461997039775"
#> [1] "sse_sqrt is: 5.60909948563715"
#> [1] "sse is: 31.541522202843"
#> [1] "sse_sqrt is: 5.61618395379309"
#> [1] "sse is: 31.5262640566299"
#> [1] "sse_sqrt is: 5.6148253807781"
#> [1] "sse is: 31.3885405555407"
#> [1] "sse_sqrt is: 5.60254768436117"
#> [1] "sse is: 31.3020839805336"
#> [1] "sse_sqrt is: 5.59482653712639"
#> [1] "sse is: 31.2394990310993"
#> [1] "sse_sqrt is: 5.58923062962151"
#> [1] "sse is: 31.1291058341569"
#> [1] "sse_sqrt is: 5.5793463626268"
#> [1] "sse is: 31.0432886806848"
#> [1] "sse_sqrt is: 5.5716504449476"
#> [1] "sse is: 30.9731313719719"
#> [1] "sse_sqrt is: 5.56535096574977"
#> [1] "sse is: 30.89807060749"
#> [1] "sse_sqrt is: 5.55860329646666"
#> [1] "sse is: 30.9664576055658"
#> [1] "sse_sqrt is: 5.5647513516388"
#> [1] "sse is: 31.0945857927386"
#> [1] "sse_sqrt is: 5.57625194846311"
#> [1] "sse is: 30.964018932261"
#> [1] "sse_sqrt is: 5.56453222942064"
#> [1] "sse is: 30.9566188668455"
#> [1] "sse_sqrt is: 5.5638672581978"
#> [1] "sse is: 30.9277576773042"
#> [1] "sse_sqrt is: 5.56127302668231"
#> [1] "sse is: 30.8856938290324"
#> [1] "sse_sqrt is: 5.55748988564373"
#> [1] "sse is: 30.9044983034542"
#> [1] "sse_sqrt is: 5.55918144185402"
#> [1] "sse is: 30.9133655218974"
#> [1] "sse_sqrt is: 5.55997891379971"
#> [1] "sse is: 30.8930571612519"
#> [1] "sse_sqrt is: 5.55815231540589"
#> [1] "sse is: 30.8990142570237"
#> [1] "sse_sqrt is: 5.55868817771097"
#> [1] "sse is: 30.8898583201585"
#> [1] "sse_sqrt is: 5.5578645467624"
#> [1] "sse is: 30.8914626281251"
#> [1] "sse_sqrt is: 5.55800887262022"
#> [1] "sse is: 30.8876313524631"
#> [1] "sse_sqrt is: 5.55766419932538"
#> [1] "sse is: 30.8896844749285"
#> [1] "sse_sqrt is: 5.5578489071698"
#> [1] "sse is: 30.887064580199"
#> [1] "sse_sqrt is: 5.55761320894132"
#> [1] "sse is: 30.8875271531406"
#> [1] "sse_sqrt is: 5.55765482493656"
#> [1] "sse is: 30.886462902909"
#> [1] "sse_sqrt is: 5.55755907777047"
#> [1] "sse is: 30.8861688123642"
#> [1] "sse_sqrt is: 5.55753261910033"
#> [1] "sse is: 30.8858791018272"
#> [1] "sse_sqrt is: 5.55750655436655"
#> [1] "sse is: 30.8858795179267"
#> [1] "sse_sqrt is: 5.55750659180237"
#> [1] "sse is: 30.8856944132636"
#> [1] "sse_sqrt is: 5.55748993820624"
#> [1] "sse is: 30.8858235999944"
#> [1] "sse_sqrt is: 5.55750156095294"
#> [1] "sse is: 30.8856965551535"
#> [1] "sse_sqrt is: 5.55749013090923"

Created on 2023-01-09 with reprex v2.0.2

See the FAQ for how to create a minimum reproducible example using reprex. That will provide a cut-and-paste way to put everyone on a common footing and also help the poster to catch different issues that can be fixed, such as missing libraries.

1 Like
df5<-data.frame(H1=c(1,5),H2=c(4,7),H3=c(12,17),H4=c(21,32),H5=c(25,41),H6=c(52,64),
                   F1=c(2,8),F2=c(9,10),F3=c(18,21),F4=c(21,32),F5=c(31,49),F6=c(59,68)) 

output<<-data.frame("experiment_num"=numeric(0),
"sse"=numeric(0),"sse_sqrt"=numeric(0),"b0"=numeric(0),"b1"=numeric(0))

lm_sse5<-function(experiment_num,dat,par){
  b0<-par[1]
  b1<-par[2]
  
  # Convert wide to long inside of function
  lm_df5<-data.frame(x=c(dat[["H1"]],dat[["H2"]],dat[["H3"]],dat[["H4"]],dat[["H5"]],dat[["H6"]]),
                     y=c(dat[["F1"]],dat[["F2"]],dat[["F3"]],dat[["F4"]],dat[["F5"]],dat[["F6"]]))
  
  yhat<-b0 + b1*lm_df5[["x"]]
  sse<- sum((yhat-lm_df5[["y"]])**2)
  sse_sqrt<-sqrt(sse)
  
  new_bit <-data.frame(experiment_num,sse,sse_sqrt,b0,b1)
  row.names(new_bit) <- NULL
  output <<- rbind(output,new_bit)
  
  print(paste0("experiment_num is: ",experiment_num))
  print(paste0("sse is: ",sse))
  print(paste0("sse_sqrt is: ", sse_sqrt))
  
  return(sse)
}
param_start<-c(b0=0,b1=0)
result_list<-sapply(1:nrow(df5),function(x) optim(param_start,
                                                  fn=lm_sse5,
                                                  experiment_num = x,
                                                  dat=df5[x,])$par ) |> t() |> data.frame()
result_list
output

Thank you. I've since edited my original post and used the reprex function (hopefully, correctly!). I wasn't aware of this function, so I greatly appreciate it.

1 Like

Thank you! This worked. However, this records each and every single iteration of the model. In my larger model with more subjects, this will take a long time to run. I really only need the final result for each experiment_num. Is there a way to record only the final values of each model fit for each experiment_num?

I would therefore make your function work differently depending if its being called many times by optim and should only calculate and return the value,vs otherwise when it will return all info.

df5<-data.frame(H1=c(1,5),H2=c(4,7),H3=c(12,17),H4=c(21,32),H5=c(25,41),H6=c(52,64),
                F1=c(2,8),F2=c(9,10),F3=c(18,21),F4=c(21,32),F5=c(31,49),F6=c(59,68)) 


lm_sse5<-function(experiment_num,dat,par,in_optim=TRUE){
  b0<-par[1]
  b1<-par[2]
  
  # Convert wide to long inside of function
  lm_df5<-data.frame(x=c(dat[["H1"]],dat[["H2"]],dat[["H3"]],dat[["H4"]],dat[["H5"]],dat[["H6"]]),
                     y=c(dat[["F1"]],dat[["F2"]],dat[["F3"]],dat[["F4"]],dat[["F5"]],dat[["F6"]]))
  
  yhat<-b0 + b1*lm_df5[["x"]]
  sse<- sum((yhat-lm_df5[["y"]])**2)
  if(in_optim)
    return(sse)
  
  sse_sqrt<-sqrt(sse)
  
  return(data.frame(experiment_num,sse,sse_sqrt,b0,b1))

}
param_start<-c(b0=0,b1=0)
result_list<-sapply(1:nrow(df5),function(x) optim(param_start,
                                                  fn=lm_sse5,
                                                  experiment_num = x,
                                                  dat=df5[x,])$par ) |> t() |> data.frame()

result_list

lapply(1:nrow(result_list),FUN = \(x)lm_sse5(experiment_num = x,
                                             dat=df5[x,],
                                             par = result_list[,x],
                                             in_optim = FALSE)) |> bind_rows()
1 Like

Wow! That is exactly what I wanted. I can't thank you enough!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.