6552

using paste() in a for loop with glm

Question:

In the code below, df.pts is a dataframe. I'd like to run about dozen glm models using different y variable (only two shown in the code). I'm using a for loop with the paste() function, but I can't seem to get the paste() function to work properly. What am I missing with paste()?

SPCA2 = df.pts[,3] CLQU2 = df.pts[,4] df.list = list(SPCA2, CLQU2) for (i in df.list) { qp.mod = glm(paste(i,"~NDVI+ELEV"), family="quasipoisson", data=my.data) print(summary(gp.mod)) }

Answer1:

Many thanks! The main problem was that df.list was a list of vectors, and should have been a list of names.

I other words, to correct the problem...

df.list = ("SPCA2", "CLQU2")

instead of

df.list = list(SPCA2, CLQU2)

However, it was also correctly pointed out that the dataframe, my.data, was not the correct dataframe. Finally, while it worked without it, the function as.formula() also worked. Again, many thanks!

Answer2:

You need to add as.formula before paste to let R know you want to treat it as a formula instead of characters.

qp.mod = glm(as.formula(paste(i,"~NDVI+ELEV")), family="quasipoisson", data=my.data)

Recommend

  • using paste() in a for loop with glm
  • PHP preg match / replace?
  • How do I expand a built in Git command with an alias?
  • Escaping of rails-generated URL
  • How to create a field class containing an image in Ext JS 4?
  • draw multiple encoded polylines using api v3
  • What's the difference with crf and qp in ffmpeg?
  • Is need install mysql on AWS EC2 if i am using AWS rds as database instance?
  • parallelize process in missForest package
  • Emacs lua-mode issue: (void-function interactively-called-p)
  • Want to understand iframe breakout code
  • Lua: Line breaks in strings
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Xcode 4 NSLog Macro link in Xcode 3
  • D3 nodes and links from JSON with nested arrays of children
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How to format a variable of double type
  • Comma separated Values
  • Why joiner is not used after Sequence generator or Update statergy
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • Is it possible to post an object from jquery to bottle.py?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?