[R] comparing linear models
I wanted to do an F-test to compare model L2 with L3 assuming heteroskedasticity. The variable KONK2 only has three levels and are for that reason replaced by dummyvariables KONKD1 and KONKD2 in L3. Hence making L4 equivalent to L2. L2-lm(PRMRES ~ factor(NYPR) + KONK2 + OMS + factor(NYPR)*OMS ) L3-lm(PRMRES ~ factor(NYPR)+ KONKD1+ KONKD2 + OMS + factor(NYPR)*OMS) L4-lm(PRMRES ~ factor(NYPR)+ I(2*KONKD2+KONKD1) + OMS + factor(NYPR)*OMS) I tried Waldtest in lmtest, but no result due to non-nested models. I don't know whether linearHypothesis for car can be of any use restricting coefficients of KONKD1 and KONKD2 to be equal. So Im a little low on ideas Also being a novice I'm not quite sure of the setup. -- View this message in context: http://r.789695.n4.nabble.com/comparing-linear-models-tp4568825p4568825.html Sent from the R help mailing list archive at Nabble.com. __ 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] Is there a way to find all roots of a polynomial equation in R?
Is there a way to find all roots of a polynomial equation? Lets say x^5+a*x^4+b*x^3+c*x^2+d*x+e=0 how to find its all roots? The package rootSolve might proove interesting __ R-help@ 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. -- View this message in context: http://r.789695.n4.nabble.com/Is-there-a-way-to-find-all-roots-of-a-polynomial-equation-in-R-tp4568892p4569100.html Sent from the R help mailing list archive at Nabble.com. __ 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] Using objectname in function
Is there anyway I can convert a vectors objectname to a string to be used in fx: Monkey-c(0,0,0,1,1,1) Wax-c(1,0,1,0,1,0) f-function(x,y){ table(x,y) } f(Monkey,Wax) so that the printout is not y x 0 1 0 1 2 1 2 1 but Wax Monkey 0 1 0 1 2 1 2 1 -- View this message in context: http://r.789695.n4.nabble.com/Using-objectname-in-function-tp2329632p2329632.html Sent from the R help mailing list archive at Nabble.com. __ 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] ifelse command
Or try: datbig-numeric(1000) for (i in 1:1000){ datbig[i] - mean(sample(1:6, 1000, prob = c(1, 1, 2, 3, 2, 1)/10, replace = TRUE)) hist(datbig,breaks=FD) -- View this message in context: http://r.789695.n4.nabble.com/ifelse-command-tp2329538p2329711.html Sent from the R help mailing list archive at Nabble.com. __ 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] ifelse command
sorry missed an } datbig-numeric(1000) for (i in 1:1000){ datbig[i] - mean(sample(1:6, 1000, prob = c(1, 1, 2, 3, 2, 1)/10, replace = TRUE)) hist(datbig,breaks=FD) } -- View this message in context: http://r.789695.n4.nabble.com/ifelse-command-tp2329538p2329717.html Sent from the R help mailing list archive at Nabble.com. __ 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] Plotproblem, histogram, y-axis label?
How can I control label on y-axis? Specifically how high relative to the y-axis it is plotted? At the moment I am using mtext appearing in the function quotet below. The function is applied to a dataframe and plots histograms for all numerical variables of the frame generating several histograms in a single window. The height of the y-axis varies and unfortunately conflicts with the yaxis label for some of the singular plots. I could change the argument padj=-7 to -8 in the function line: mtext(Density,side=2,cex=0.7,padj=-7,line=0,las=1) but then Density is plotted to high for the other graphs. Is there a way I can extract information on the hight of the y-axis and put this to use in controlling the padj-value or is there some other strategy?? I have tried changing the side-argument to side=3 as in mtext(Density,at=0,side=3,cex=0.7,line=0,las=1) But now something really strange (to me anyway http://r.789695.n4.nabble.com/file/n2328545/image.jpg ) happens - as shown in image: In the first row of histograms - with 3 columns - the Density label is fitted nicely above the y axis. But in the second row the Density label do not appear, the when I make the plotwindow thinner - adjusting viewsize with mouse - they start to appear. When I make the window wider the label of the histogram in the second row second column moves to the left until it is above the label of the histogram 2 row 1 column. When I continue to make the window wider the labels of both mentioned histograms moves entirely out of view. This is what the image.jpg shows A secondary problem is that when i save the plot as .eps and later view it adobe illustrater or GSview4.9 although the y-label did not conflict with the axis as shown in R-plotting window it now conflicts with the axis. The entire function used is as follows: histogram-function(x){ Counts the number of numeric variables in dataset, and adjust plotframe accordingly dataclass-sapply(x,class) count-sum((dataclass==numeric | dataclass==integer)) par(mfrow=c(ceiling(count/3),3)) Following code decides the design of the individual histogram plots f-function(x,col.name){ list-density(x) y-hist(x,breaks=FD,probability=TRUE,border=white,col=rgb(180,180,180,maxColorValue=255),las=1,ylab=,ylim=c(0,max(list$y)),xlab=col.name, main=) lines(density(x),col=steelblue4,lwd=1) lines(quantile(x,c(0.025,0.975)),c(0,0),lwd=2,col=steelblue4) mtext(Density,side=2,cex=0.7,padj=-7,line=0,las=1) text(x=quantile(x,c(0.025,0.975)),y=c(max(y$density)/12,max(y$density)/12),labels=as.character(quantile(x,c(0.05,0.95))), col=steelblue4,cex=0.8) } Following code applies f - drawing of histogram - to all numeric variables in dataframe for (i in 1:length(x)) {if (is.numeric(x[,i])) { col.name-names(x[i]) f(x[,i],col.name=col.name)} } } Best regards Jesper Hybel -- View this message in context: http://r.789695.n4.nabble.com/Plotproblem-histogram-y-axis-label-tp2328545p2328545.html Sent from the R help mailing list archive at Nabble.com. __ 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] Learning ANOVA
how to plot 4 graphs simultaneously? Depends on what graphs were talking about: A Side-by-side boxplot containing fx 4 boxplots can be generated by a single plot() command. However often you use a primary command that activivates the plot window and plots one graph, then afterwards you use secondary commands to add new graphical objects to the same graph/window: x-c(1,2,3,4) y-c(1,2,3,4) plot(x,y) #primary graphical command activates plot window abline(-1,3) # adds line points(c(1,2),c(2,2),col=red,pch=16) #adds points Also how to save InsectSprays.aov? I think I can only save it as InsectSprays.csv. I can't find write.aov command. InsectSprays.aov is an object - in this case probably a list. If you make an assignment using the assignment operator '-' to an object not already existing in you're current work session R will create that object. Or to put it differently: InsectSprays.aov is only a name you might as well have called it HomeGrownMagic or McDonalds - the aov in the name is not to be mistaken for a file extension. The name only surves the purpose to remind you what it is a container of namely an ANOVA output of the variables Insect and Spray, that helps you not youre computer. There are different ways to save: You can store the whole worksession, thereby storing all the objects you have created during worksession and have not removed. Or you can sink particular objects I recommend that you have a look at: http://cran.r-project.org/doc/contrib/Verzani-SimpleR.pdf Very well written, want tell you all you want to know but will not drown you in computer lingo and tie you down with details. Gives you some hands on experience handling the most basic plots and objects in R. If you later want to buy Verzanis book grown from the freely available notes you go wrong there either, basically more of the same. -- View this message in context: http://r.789695.n4.nabble.com/Learning-ANOVA-tp2323660p2325057.html Sent from the R help mailing list archive at Nabble.com. __ 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] How to perform a substitution in a loop?
You need {} for (i in 1:500){ ...commands...} Like: x-rep(c(0.8,1.2),250) for (i in 1:500) { if (x[i]1) x[i]=1 } BR JEsper -- View this message in context: http://r.789695.n4.nabble.com/How-to-perform-a-substitution-in-a-loop-tp2325048p2325073.html Sent from the R help mailing list archive at Nabble.com. __ 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] Linear regression on several groups
Example is spot on - sr for not providing one myself. The results you calculate are what I'm looking for. Would like a function F where I could type: F(weight ~ Time, data = ChickWeight, SOME ARGUMENT = Diet)) Resulting in for (i in 1:4){ print( lm(weight ~ Time, data = ChickWeight, subset = Diet==i) ) } I'm simply trying to minimize amount of looping and typing etc. BR Jesper -- View this message in context: http://r.789695.n4.nabble.com/Linear-regression-on-several-groups-tp2322835p2324154.html Sent from the R help mailing list archive at Nabble.com. __ 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] Learning ANOVA
If you're trying to follow the youtube video you have a typing mistake here: InsectSprays.aov -(test01$count ~ test01$spray) I think this should be: InsectSprays.aov -aov(test01$count ~ test01$spray) youre missing the functioncall aov on the right hand side of the assignment operator '-'. the results of the application of function aov() is stored in InsectSprays.aov and is accessed through summary(InsectSprays.aov) since you missed the functioncall you cannot apply TukeyHSD() to InsectSprays.aov I think -- View this message in context: http://r.789695.n4.nabble.com/Learning-ANOVA-tp2323660p2324590.html Sent from the R help mailing list archive at Nabble.com. __ 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] merge function in R?
I think it would be helpful if you could clarify youre question - do you want distinct sets - maybe use unique() but why (5,20) when its (5,10) in the row in youre example? What criteria do you want the function to select the sets by and what kind of output do you need? Maybe it's just me who dosn't get the question..sr -- View this message in context: http://r.789695.n4.nabble.com/merge-function-in-R-tp2324684p2324844.html Sent from the R help mailing list archive at Nabble.com. __ 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] Linear regression on several groups
I have a simple dataset of a numerical dependent Y, a numerical independent X and a categorial variable Z with three levels. I want to do linear regression Y~X for each level of Z. How can I do this in a single command that is without using lm() applied three isolated times? -- View this message in context: http://r.789695.n4.nabble.com/Linear-regression-on-several-groups-tp2322835p2322835.html Sent from the R help mailing list archive at Nabble.com. __ 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] Creating vectors
You can use following scriptI think #create a vector of random numbers on which to test script v-sample(1:3,size=90,replace=TRUE) #creates two matrixes out of vector v which can be assigned to M to test script M2-matrix(v,ncol=2) M3-matrix(v,ncol=3) M- #Assign you're matrix or a testmatrix to M and run script result-numeric() imaks-length(M[,1]) jmaks-length(unique(M)[,1]) for (i in 1:imaks){ for (j in 1:jmaks){ if (sum(M[i,]==unique(M)[j,])==length(M[1,])) { result[i]-j } } } result ## The script uses loops so its not efficient - in other words its slow and might be too slow for larger matrixes. BR Jesper -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2323090.html Sent from the R help mailing list archive at Nabble.com. __ 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.