0

I have to quote very long string in R. I use quote function. But, it return only certain length of tags to the console. Does anyone know the way to go around that.

S <- quote(prfl1 * X1 + prfl2 * X2 + prfl3 * X3 + prfl4 * X4 + prfl5 * X5 + prfl6 * X6 + prfl7 * X7 + prfl8 * X8 + prfl9 * X9 + prfl10 * X10 + prfl11 * X11 + prfl12 * X12 + prfl13 * X13 + prfl14 * X14 + prfl15 * X15 + prfl16 * X16 + prfl17 * X17 + prfl18 * X18 + prfl19 * X19 + prfl20 * X20 + prfl21 * X21 + prfl22 * X22 + prfl23 * X23 + prfl24 * X24 + prfl25 * X25 + prfl26 * X26 + prfl27 * X27 + prfl28 * X28 + prfl29 * X29 + prfl30 * X30 + prfl31 * X31 + prfl32 * X32 + prfl33 * X33 + prfl34 * X34 + prfl35 * X35 + prfl36 * X36 + prfl37 * X37 + prfl38 * X38 + prfl39 * X39 + prfl40 * X40 + prfl41 * X41 + prfl42 * X42 + prfl43 * X43 + prfl44 * X44 + prfl45 * X45 + prfl46 * X46 + prfl47 * X47 + prfl48 * X48 + prfl49 * X49 + prfl50 * X50 + prfl51 * X51 + prfl52 * X52 + prfl53 * X53 + prfl54 * X54 + prfl55 * X55 + prfl56 * X56 + prfl57 * X57 + prfl58 * X58 + prfl59 * X59 + prfl60 * X60 + prfl61 * X61 + prfl62 * X62 + prfl63 * X63 + prfl64 * X64 + prfl65 * X65 + prfl66 * X66 + prfl67 * X67 + prfl68 * X68 + prfl69 * X69 + prfl70 * X70 + prfl71 * X71 + prfl72 * X72 + prfl73 * X73 + prfl74 * X74 + prfl75 * X75 + prfl76 * X76 + prfl77 * X77 + prfl78 * X78 + prfl79 * X79 + prfl80 * X80 + prfl81 * X81 + prfl82 * X82 + prfl83 * X83 + prfl84 * X84 + prfl85 * X85 + prfl86 * X86 + prfl87 * X87 + prfl88 * X88 + prfl89 * X89 + prfl90 * X90 + prfl91 * X91 + prfl92 * X92 + prfl93 * X93 + prfl94 * X94 + prfl95 * X95 + prfl96 * X96 + prfl97 * X97 + prfl98 * X98 + prfl99 * X99 + prfl100 * X100 + prfl101 * X101 + prfl102 * X102 + prfl103 * X103 + prfl104 * X104 + prfl105 * X105 + prfl106 * X106 + prfl107 * X107 + prfl108 * X108 + prfl109 * X109 + prfl110 * X110 + prfl111 * X111 + prfl112 * X112 + prfl113 * X113 + prfl114 * X114 + prfl115 * X115 + prfl116 * X116 + prfl117 * X117 + prfl118 * X118 + prfl119 * X119 + prfl120 * X120 + prfl121 * X121 + prfl122 * X122 + prfl123 * X123 + prfl124 * X124 + prfl125 * X125 + prfl126 * X126 + prfl127 * X127 + prfl128 * X128 + prfl129 * X129 + prfl130 * X130 + prfl131 * X131 + prfl132 * X132 + prfl133 * X133 + prfl134 * X134 + prfl135 * X135 + prfl136 * X136 + prfl137 * X137 + prfl138 * X138 + prfl139 * X139 + prfl140 * X140 + prfl141 * X141 + prfl142 * X142 + prfl143 * X143 + prfl144 * X144 + prfl145 * X145 + prfl146 * X146 + prfl147 * X147 + prfl148 * X148 + prfl149 * X149 + prfl150 * X150 + prfl151 * X151 + prfl152 * X152 + prfl153 * X153 + prfl154 * X154 + prfl155 * X155 + prfl156 * X156 + prfl157 * X157 + prfl158 * X158 + prfl159 * X159 + prfl160 * X160 + prfl161 * X161 + prfl162 * X162 + prfl163 * X163 + prfl164 * X164 + prfl165 * X165 + prfl166 * X166 + prfl167 * X167 + prfl168 * X168 + prfl169 * X169 + prfl170 * X170 + prfl171 * X171 + prfl172 * X172 + prfl173 * X173 + prfl174 * X174 + prfl175 * X175 + prfl176 * X176 + prfl177 * X177 + prfl178 * X178 + prfl179 * X179 + prfl180 * X180 + prfl181 * X181 + prfl182 * X182 + prfl183 * X183 + prfl184 * X184 + prfl185 * X185 + prfl186 * X186 + prfl187 * X187 + prfl188 * X188 + prfl189 * X189 + prfl190 * X190 + prfl191 * X191 + prfl192 * X192 + prfl193 * X193 + prfl194 * X194 + prfl195 * X195 + prfl196 * X196 + prfl197 * X197 + prfl198 * X198 + prfl199 * X199 + prfl200 * X200 + prfl201 * X201 + prfl202 * X202 + prfl203 * X203 + prfl204 * X204 + prfl205 * X205 + prfl206 * X206 + prfl207 * X207 + prfl208 * X208 + prfl209 * X209 + prfl210 * X210 + prfl211 * X211 + prfl212 * X212 + prfl213 * X213 + prfl214 * X214 + prfl215 * X215 + prfl216 * X216 + prfl217 * X217 + prfl218 * X218 + prfl219 * X219 + prfl220 * X220 + prfl221 * X221 + prfl222 * X222 + prfl223 * X223 + prfl224 * X224 + prfl225 * X225 + prfl226 * X226 + prfl227 * X227 + prfl228 * X228 + prfl229 * X229 + prfl230 * X230 + prfl231 * X231 + prfl232 * X232 + prfl233 * X233 + prfl234 * X234 + prfl235 * X235 + prfl236 * X236 + prfl237 * X237 + prfl238 * X238 + prfl239 * X239 + prfl240 * X240 + prfl241 * X241 + prfl242 * X242 + prfl243 * X243 + prfl244 * X244 + prfl245 * X245 + prfl246 * X246 + prfl247 * X247 + prfl248 * X248 + prfl249 * X249 + prfl250 * X250 + prfl251 * X251 + prfl252 * X252 + prfl253 * X253 + prfl254 * X254 + prfl255 * X255 + prfl256 * X256 + prfl257 * X257 + prfl258 * X258 + prfl259 * X259 + prfl260 * X260 + prfl261 * X261 + prfl262 * X262 + prfl263 * X263 + prfl264 * X264 + prfl265 * X265 + prfl266 * X266 + prfl267 * X267 + prfl268 * X268 + prfl269 * X269 + prfl270 * X270 + prfl271 * X271 + prfl272 * X272 + prfl273 * X273 + prfl274 * X274)

This returns values juts to prfl253

Mateusz1981
  • 1,817
  • 17
  • 33
  • 2
    A good programmer wouldn't want to write code like that. You should explain what you actually want to achieve. (You have heard of matrix multiplication, right?) – Roland Jun 24 '15 at 13:34
  • Very long string indeed. That being said, your code works for me. What text editor are you using? You may try copy-pasting that directly into the R terminal ... you might be experiencing a problem with your text editor passing that to R. Just a wild guess. – rbatt Jun 24 '15 at 13:35
  • I generate this string in R and than paste from R console to `quote`. It is more into statistics. I have few variables S in my model which I want to replace by quoted S. This is a multiplication of dummy variable which is in the data and unknown parameter `X` – Mateusz1981 Jun 24 '15 at 13:40
  • Roland has a point. Given your obvious pattern of multiplications, there's probably a better way to write this than every single pair from 1 to 274. – Molx Jun 24 '15 at 13:44
  • it is in Connection to my question [link](http://stackoverflow.com/questions/30728437/how-to-quote-this-in-r) where I made a function to generate the string but I could not `quote` it. – Mateusz1981 Jun 26 '15 at 08:50

1 Answers1

1

Apparently we have an XY problem here. I've looked at your other question and your goal seems to be to do nonlinear regression with this formula:

BH100 ~ S * ((1 - exp(-b2*TIME))/(1-exp(-b2*100)))^b3

where S is a linear combination of dummy variables (encoding one categorical variable).

First of all, you don't need to quote anything. You need to create a formula object.

Your example data:

DF <- structure(list(year = c(1987L, 1990L, 1994L, 1983L, 1986L, 1990L, 
  1998L, 2002L, 1994L, 1998L, 2002L, 2008L, 2013L, 1985L, 1989L, 
  1990L), T = c(25L, 28L, 32L, 21L, 24L, 28L, 36L, 40L, 32L, 36L, 
  40L, 46L, 51L, 23L, 27L, 28L), BH100 = c(12.6, 14.9, 18.8, 13.4, 
  16.1, 19.6, 26.7, 27.8, 17.2, 19.4, 23.5, 26.3, 28.7, 14.6, 18.5, 
  19.2), TIME = c(25L, 28L, 32L, 21L, 24L, 28L, 36L, 40L, 32L, 
  36L, 40L, 46L, 51L, 23L, 27L, 28L), dum1 = c(1L, 1L, 1L, 0L, 
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), dum2 = c(0L, 
  0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
  dum3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 
    1L, 0L, 0L, 0L), dum4 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L)), 
  .Names = c("year", "T", "BH100", "TIME", "dum1", "dum2", "dum3", "dum4"), 
  class = "data.frame", row.names = c(NA, -16L))

Now we create a formula (I show here how to use partially linear least-squares for the problem, for this you need to work at the log scale):

fo <- as.formula(paste0("log(BH100) ~ cbind(",
                        paste(names(DF)[5:8], collapse = ", "),
                        ", log(((1 - exp(-b2*TIME))/(1-exp(-b2*100)))))"))
#log(BH100) ~ cbind(dum1, dum2, dum3, dum4, log(((1 - exp(-b2 * 
#    TIME))/(1 - exp(-b2 * 100)))))

coefs1 <- coef(fit)
#          b2    .lin.dum1    .lin.dum2    .lin.dum3    .lin.dum4        .lin5 
#1.200000e+00 2.724458e+00 3.100260e+00 3.118730e+00 2.867354e+00 4.638915e+10 

Obviously this isn't a good fit because of the tiny amount of data.

You can then use gnls to achieve a fit at the original scale. First you have to combine the dummies into a factor variable:

library(reshape2)
DF <- melt(DF, id.vars = 1:4)
DF <- DF[DF$value == 1L, -6]

And now you can fit the model:

library(nlme)
fit <- gnls(BH100 ~ a * ((1 - exp(-b2*TIME))/(1-exp(-b2*100)))^b3,
            data = DF,
            params = list(a ~ variable, b2 ~ 1, b3 ~ 1),
            start = list(b2 = coefs1["b2"], b3 = tail(coefs1, 1), 
                         a = exp(coefs1[-c(1, length(coefs1))])))
#Error in gnls(BH100 ~ a * ((1 - exp(-b2 * TIME))/(1 - exp(-b2 * 100)))^b3,  : 
#  approximate covariance matrix for parameter estimates not of full rank

Here I specified a linear model for the parameter a. Conveniently R does the dummy encoding for you. Of course, this doesn't result in a successful fit because you would need more data (and better starting values) than you provided for such a complicated model.

Community
  • 1
  • 1
Roland
  • 127,288
  • 10
  • 191
  • 288
  • I'll test soon the solution you provided. – Mateusz1981 Aug 03 '15 at 13:13
  • hi, it was long time ago. I tried the solution by @Roland and I did not succeed. I do not why as it looks ok. I got error `step halving factor reduced below minimum in NLS step`. Wrong starting values? I don't know. When I did this example with the `quote` than it worked ok. I used the same starting values in `gnls` and it failed. @Roland, can you add the code for fitting `fo`. In the example there are just 4 dummies, what to do if I have 200, do I have to type in each starting value one by one? – Mateusz1981 Nov 03 '15 at 09:36
  • If you have 200 factor levels, you should probably not model these as fixed effects. Such a model will be over-parameterized. Instead you should probably use a non-linear mixed effects model and model them as a random effect. – Roland Nov 03 '15 at 17:22
  • but this is a well established method in the field of site index modeling in forestry. check this document [http://pmrc.uga.edu/TR2000-7.pdf], chepter 4 and 5 – Mateusz1981 Nov 04 '15 at 08:17
  • You are free to ignore my advice. However, using only well established methods doesn't really improve science. And you obviously have problems to support a 202-parameter model with your data. And of course, non-linear mixed models can be considered a well established method, too. – Roland Nov 04 '15 at 18:34
  • It was not my intention to ignore your comment and I agree that the mixed models should work in this case as well. Just want to understand this. Is not dummy method almost the same as mixed models? One calculates the unique `S` for each plot and other parameters are common for all plots? – Mateusz1981 Nov 05 '15 at 07:23