[R] Question about copula-GARCH model
Hello everyone, I am learning about copulas and also do some MATLAB/R coding to get better understanding of how copulas work. Recently I have started coding simple copula-GARCH models, that is I fit say AR(1)-GARCH(1,1)-normal models to univariate time series, and then I want to fit the copula (two-stage procedure). What I have problem with is connecting these two estimation stages. After I have estimated AR-GARCH univariate models, what do I take from these models and put into log-likelihood estimation of the copula? Do I take residuals from AR-GARCH models, or do I use estimated parameters of these models to produce samples that I then use in copula estimation stage? I read a few papers that use copula-GARCH models, but it is not clear from them how to estimate copula model. In one of the papers it says: Let u=F(x; theta(x)) and v=F(y; theta(y)), where theta(x) and theta(y) are the vectors of parameters of each marginal distribution... and then one uses u and v in copula log-likelihood minimization. I am so embarassed, but I still do not get it. If I estimated the GARCH model parameters, how do I get these F(x; theta(x)) and F(y; theta(y))? Probably very simple and totally obvious thing, but I just do not get it. :-( Could you please help me understand? How do I do it in MATLAB or R? THanks in advance! -- Jonas Malmros __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to remove double for loop?
Hello everyone. I use double for loops to fill in matrices, but there are surely better (and computationally faster) ways to perform that task. Could someone show me, given the following example of a double for loop, how this could be done? It is much easier to learn by examples. Val - matrix(0, nrow=n+1, ncol=n+1) for( i in 0:n){ for(j in 0:i){ Val[j+1, i+1] - u^j*d^(i-j) } } Thank you in advance! -- Jonas Malmros __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to remove double loop?
Bill, Alberto, Gabor, Thank you for answering my question. Now I learned about outer() function. That was a straightforward example. But what if I had a matrix, where the last column was filled with values first (again, a for loop), and the rest was filled by using a double loop? OVal - matrix(0, n+1, n+1) for(i in 0:n){ OVal[i+1, n+1] - max(Val[i+1, n+1]-K, 0) } for(i in seq(n,1, by=-1)){ for(j in 0:(i-1)){ OVal[j+1, i] - a*((1-p)*OVal[j+1, i+1]+p*OVal[j+2, i+1]) } } Even if I leave the first simple for loop as it is, is there a more efficient way to program the double loop part now that OVal is used within the function itself? It is pretty easy to write for loops, but it is very hard to write computationally optimal code. :-( Could you please help me with the above one, if possible? -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Polynomial fitting
Dear Mr. Rowlingson, Rizopoulos, Jaworski, and Ripley Thank you for your help with the polynomial. Regards, Jonas On Jan 7, 2008 5:18 PM, Barry Rowlingson [EMAIL PROTECTED] wrote: Dimitris Rizopoulos wrote: try this: y - c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 11.32) x - seq(3.75, 6, 0.25) coef(lm(y ~ x + I(x^2) + I(x^3))) Or use the 'poly' function: coef(lm(x~poly(y,3))) (Intercept) poly(y, 3)1 poly(y, 3)2 poly(y, 3)3 4.875 -0.6233293 0.0312415 -0.6464533 But hmmm those aren't the same coefficients? Because you need to use 'raw=TRUE' if you really want to fit the raw powers rather than orthogonal polynomials (which are better behaved than fitting the raw powers): coef(lm(y~poly(x,3,raw=TRUE))) (Intercept) poly(x, 3, raw = TRUE)1 poly(x, 3, raw = TRUE)2 -774.258364 472.172611 -91.633939 poly(x, 3, raw = TRUE)3 5.800653 And there's your coefficients. See help(poly) for more. Barry -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Polynomial fitting
I wonder how one in R can fit a 3rd degree polynomial to some data? Say the data is: y - c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 11.32) x - seq(3.75, 6, 0.25) And resulting degrees of polynomial are: 5.8007 -91.6339 472.1726 -774.2584 THanks in advance! -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Diagonal matrix with off diagonal elements
Hi, everyone I wonder if there is a function in R with which I can create a square matrix with elements off main diagonal (for example one diagonal below the main diagonal). Thanks in advance! -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to create a mixed col.names?
Hello, I have a vector of names, say : names - c(Factor 1, Factor 2, Factor 3) I am creating a dataframe and I want the column names to be mixed like this: Factor 1 Sign Factor 1 Factor 2 Sign Factor 2 Factor 3 Sign Factor 3 How can I automate the creation of such a mixed vector? I tried with rep but did not succeed. Could someone please suggest a solution to this problem? Thanks in advance! Regards, JM -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Cannot grasp how to apply by here...
I have a data frame named database with panel data, a little piece of which looks like this: Symbol Name TrialFactor1 Factor2 External 1 548140 A 1-3.87 -0.32 0.01 2 547400 B 112.11 -0.68 0.40 3 547173 C 1 4.50 0.71-1.36 4 546832 D 1 2.59 0.00 0.09 5 548140 A 2 2.41 0.50-1.04 6 547400 B 2 1.87 0.32 0.39 What I want to do is to calculate correlation between each factor and external for each Symbol, and record the corr. estimate, the p.value, the name and number of observations in a vector named vector, then rbind these vectors together in results. When there are fewer than 5 observations for a particular symbol I want to put NAs in each column of vector. I tried with the following code, making assumption that by splits database into sort of smaller dataframes for each Symbol (that's the x): factor.names - c(Factor1, Factor2) factor.pvalue - c(SigF1, SigF2) results - numeric() vector - matrix(0, ncol=(length(factor.names)*2+2), nrow=1) colnames(vector) - c(No.obs, factor.names, factor.pvalue) application - function(x){ rownames(vector) - x$Name for(i in 1:length(factor.names)){ if(dim(x)[1]=5){ vector[1] - dim(x)[1] vector[i+1] - cor.test(x$External, x[,factor.names[i]], method=kendall)$estimate vector[i+3] - cor.test(x$External, x[,factor.names[i]], method=kendall)$p.value } else { vector - rep(NA, length(vector)) } } results - rbind(results, vector) } by(database, database$Symbol, application) This did not work. I get : Error in dimnames(x) - dn : length of 'dimnames' [1] not equal to array extent I used browser() and I see that the Name is not assigned to the row name of vector and then dim(x)[1] does not work. What am I doing wrong? Do not understand. :-( Thank you in advance for your help. Regards, JM -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Cannot grasp how to apply by here...
Obviously, A cannot assign a row name because the dimensions do not agree. I can use rownames(vector) - x$Name[1] though. then things get calculated (I saw it with browser()) but rbind does not do what I want it to do, results remains numeric(). why? -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Cannot grasp how to apply by here...
Dear Charilaos, Thanks, I see what you mean, but I just simplified the real code here and I made a mistake by putting +2, it is +1. :-) JM On Dec 17, 2007 8:02 PM, Charilaos Skiadas [EMAIL PROTECTED] wrote: On Dec 17, 2007, at 1:47 PM, Jonas Malmros wrote: factor.names - c(Factor1, Factor2) factor.pvalue - c(SigF1, SigF2) results - numeric() vector - matrix(0, ncol=(length(factor.names)*2+2), nrow=1) colnames(vector) - c(No.obs, factor.names, factor.pvalue) If you look at vector you'll see it has column dimension 6. You are trying to assign to it 5 colnames, which is not going to work. That's exactly what the error tells you, and it happens on the line above, the rest of your code is irrelevant to it. Haris Skiadas Department of Mathematics and Computer Science Hanover College -- Jonas Malmros Stockholm University Stockholm, Sweden -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Why is conversion not working?
I have a data frame, where two last columns - month and year - are character vectors. The year vector is made of two numbers (i.e. 97 for 1997, 07 for 2007, etc) What I want to do is to create a variable Year that is mode numeric and where each record is a four-figure number (1997, 2007,...) I have about 4 rows in the dataframe, the observations are for 10 years (so there are multiple rows for each year). I tried the following, but the program runs and runs, and if I abort it all the years in Year are 1997: for(i in 1:dim(database)[1]){ if(database$year[i]90) { database$Year[i] - as.numeric(database$year[i])+1900 } else { database$Year[i] - as.numeric(database$year[i])+2000 } } Thanks in advance for explanations. Regards, JM -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Cleaning database: grep()? apply()?
Dear R users, I have a huge database and I need to adjust it somewhat. Here is a very little cut out from database: CODENAME DATE DATA1 4813ADVANCED TELECOM19870.013 3845ADVANCED THERAPEUTIC SYS LTD198710.1 3845ADVANCED THERAPEUTIC SYS LTD19892.463 3845ADVANCED THERAPEUTIC SYS LTD19881.563 2836ADVANCED TISSUE SCI -CL A 19870.847 2836ADVANCED TISSUE SCI -CL A 1989 0.872 2836ADVANCED TISSUE SCI -CL A 1988 0.529 What I need is: 1) Delete all cases containing -CL A (and also -OLD, -ADS, etc) at the end 2) Delete all cases that have less than 3 years of data 3) For each remaining case compute ratio DATA1(1989) / DATA1(1987) [and then ratios involving other data variables] and output this into new database consisting of CODE, NAME, RATIOs. Maybe someone can suggest an effective way to do these things? I imagine the first one would involve grep(), and 2 and 3 would involve apply family of functions, but I cannot get my mind around the actual code to perform this adjustments. I am new to R, I do write code but usually it consists of for-functions and plotting. I would much appreciate your help. Thank you in advance! -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] textplot() in gplots causes problems (0x9)
Dear Gregory, How can I avoid using tab character when all I want to do is to print a model summary on my pdf device using textplot()? How do I set the font size? If you mean using cex inside textplot, then it does not work. Whether cex is 1 or 0.2 I get the same result, exemplified here: Call: lm(formula =...) Resuduals: ... Coefficients Estimate (intercept)1.32 ...... ...... Std.Error t-value (Intercept)0.2 0.1 ......... ......... Pr(|t|) (intercept) 0.01 ...... ...... −−− Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.748... Is there no solution to this problem? I am using Vista, R2.6.0 patched, RWinEdt. textplot(capture.output(summary(.model)), valign=top, halign=left, cex=0.5) Thanks in advance, Jonas On 11/1/07, Gregory Warnes [EMAIL PROTECTED] wrote: Hi Jonas, By default, textplot() attempts to automatically select a font size that is 'just big enough, but not too big'. It does this by a binary- search approach where it sets a font size, then asks R to compute the actual width of the text to be displayed (without actually displaying it), then increases or decreases the font size appropriately until it finds the largest font that doesn't extend beyond the plot region vertically or horizontally. It appears that on your system, R doesn't know how wide a tab character is. This isn't particularly surprising since tab characters vary in width depending on the context. There are two simple solutions. First, avoid using characters R can't figure out sizes for (i.e. tab), or manually specify the font size so textplot() doesn't attempt to optimize it. I personally choose the former, avoid tab characters, since the appropriate font size varies greatly by device. -Greg On Oct 31, 2007, at 3:22PM , Jonas Malmros wrote: Hello, I am using textplot function in gplots package to put some model output inside a PDF file, but it does not seem to work properly with PDF. I am doing follwing: pdf(file=C:/..., paper=a4, width=8, height=12) .model - lm(.model.formula, data=database) textplot(capture.output(summary(.model)), valign=top, halign=left) I am getting these error messages: Warning messages: 1: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 2: In strwidth(object, cex = cex) : font width unknown for character 0x9 3: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 4: In strwidth(object, cex = cex) : font width unknown for character 0x9 5: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 6: In strwidth(object, cex = cex) : font width unknown for character 0x9 7: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 8: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 This is a tab character that causes problems, I guess. Is there any way to solve this? Thank you in advance -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] textplot() in gplots causes problems (0x9)
Dear Gregory and members of R-Help I found what was causing the problem, it was the fact that my R Console window is minimized by me horizontally, and therefore summary output does not fit and gets split, and function capture.output() copies that into PDF! I maximized R Console window so that summary(model) output fits on the screen, and though I still get ërrors 2: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 3: In strwidth(object, cex = cex) : font width unknown for character 0x9 4: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 5: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 in PDF everything looks fine! Thank you for your time! Jonas. On 11/1/07, Jonas Malmros [EMAIL PROTECTED] wrote: Dear Gregory, How can I avoid using tab character when all I want to do is to print a model summary on my pdf device using textplot()? How do I set the font size? If you mean using cex inside textplot, then it does not work. Whether cex is 1 or 0.2 I get the same result, exemplified here: Call: lm(formula =...) Resuduals: ... Coefficients Estimate (intercept)1.32 ...... ...... Std.Error t-value (Intercept)0.2 0.1 ......... ......... Pr(|t|) (intercept) 0.01 ...... ...... --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.748... Is there no solution to this problem? I am using Vista, R2.6.0 patched, RWinEdt. textplot(capture.output(summary(.model)), valign=top, halign=left, cex=0.5) Thanks in advance, Jonas On 11/1/07, Gregory Warnes [EMAIL PROTECTED] wrote: Hi Jonas, By default, textplot() attempts to automatically select a font size that is 'just big enough, but not too big'. It does this by a binary- search approach where it sets a font size, then asks R to compute the actual width of the text to be displayed (without actually displaying it), then increases or decreases the font size appropriately until it finds the largest font that doesn't extend beyond the plot region vertically or horizontally. It appears that on your system, R doesn't know how wide a tab character is. This isn't particularly surprising since tab characters vary in width depending on the context. There are two simple solutions. First, avoid using characters R can't figure out sizes for (i.e. tab), or manually specify the font size so textplot() doesn't attempt to optimize it. I personally choose the former, avoid tab characters, since the appropriate font size varies greatly by device. -Greg On Oct 31, 2007, at 3:22PM , Jonas Malmros wrote: Hello, I am using textplot function in gplots package to put some model output inside a PDF file, but it does not seem to work properly with PDF. I am doing follwing: pdf(file=C:/..., paper=a4, width=8, height=12) .model - lm(.model.formula, data=database) textplot(capture.output(summary(.model)), valign=top, halign=left) I am getting these error messages: Warning messages: 1: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 2: In strwidth(object, cex = cex) : font width unknown for character 0x9 3: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 4: In strwidth(object, cex = cex) : font width unknown for character 0x9 5: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 6: In strwidth(object, cex = cex) : font width unknown for character 0x9 7: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 8: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 This is a tab character that causes problems, I guess. Is there any way to solve this? Thank you in advance -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- Jonas Malmros Stockholm University Stockholm, Sweden -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] textplot() in gplots causes problems (0x9)
Hello, I am using textplot function in gplots package to put some model output inside a PDF file, but it does not seem to work properly with PDF. I am doing follwing: pdf(file=C:/..., paper=a4, width=8, height=12) .model - lm(.model.formula, data=database) textplot(capture.output(summary(.model)), valign=top, halign=left) I am getting these error messages: Warning messages: 1: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 2: In strwidth(object, cex = cex) : font width unknown for character 0x9 3: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 4: In strwidth(object, cex = cex) : font width unknown for character 0x9 5: In FUN(c(C, a, l, l, :, l, m, (, f, o, r, : font width unknown for character 0x9 6: In strwidth(object, cex = cex) : font width unknown for character 0x9 7: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 8: In text.default(x = xpos, y = ypos, labels = object, adj = c(0, : font width unknown for character 0x9 This is a tab character that causes problems, I guess. Is there any way to solve this? Thank you in advance -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Attaching interactive plot to existing PDF file
Dear list members, I create a pdf file with a number of barplots and histograms: pdf(file=my.pdf.file, paper=a4, width=6, height=10) ... dev.off() Then I run a model and plot fitted vs residuals and interactively (using identify()) label some of the residuals on that plot. After that I want to append this interactive plot to the already existing PDF file. How can I do that? I tried opening new device with x11() then plotting fitted vs residuals in it and then (since pdf device is still open) dev.copy(dev.cur(), which=dev.prev()) but this produces error message : Error in dev.copy(dev.cur(), which = dev.prev()) : cannot supply 'which' and 'device' at the same time and when I close off the devices, my PDF file contains only barplots and histograms. Could someone please instruct me? Thank you in advance for your time and help. -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] glm with Student t for error distribution
Hello, My response variable seems to be distributed according to Student t with df=4. I have 320 observations and about 20 variables. I am wondering whether there is a way to fit glm with Student t for error distribution. Student t is not one of the family choices in glm function. How should I proceed to fit glm with Student t? I know that Student t is the Inverse Gamma with shape parameter equal to degrees of freedom (=4). Would it be correct then to specify Gamma family and inverse link in the glm function? Thank you for your help. Jonas M. University of Stockholm -- Jonas Malmros Stockholm University Stockholm, Sweden __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.