I would like to build a very simple rectangular surface in R that would have a logistic trend. The values at the top would have the highest values (1) and at the bottom the lowest (0). I have drafted an image that shows example of the surface that I have in mind, with help of not the prettiest trend lines so you have an idea what is needed. I do not have any data, it is supposed to be a theoretical surface with logistic trend, that I am later going to modify.
Any help with how to start/approach it, or helpful packages in R would be highly appreciated!
Consider this as a hint.
library("graphics")
plot(0:1, type = "n",xaxt="n", ann=FALSE)
abline(h = c(seq(0,1,.1))
or
abline(h = c(0,.1,.2,.3,.6,.7,.8,.9))
abline(h = c(0.4,.5), col="red")
The only thing you have to do is place the variable, as you call it, with the “logistic trend,” in place of ‘0:1’
A second hint
df = as.matrix(c(0.131313131,0.111111111,0.090909091,
0.080808081,0.060606061,0.050505051,
0.060606061,0.080808081,0.090909091,
0.111111111,0.131313131))
barplot(prop.table(df, 2) )
this results in
Related
I am learning how to do PCoA, but when I test metaMDS, the result is different.
NMDS<-metaMDS(eurodist)
plot(NMDS)
NMDS<-metaMDS(as.dist(eurodist))
plot(NMDS$points)
http://www.davidzeleny.net/anadat-r/doku.php/en:pcoa_nmds this is where I learned.
I thought MDS is for PCoA, while NMDS is not, dose the above sample mean metaMDS can do both MDS and NMDS?
You first problem is that plot(NMDS$points) is not creating the plot correctly; you must draw the plot with equal axis scaling or aspect ratio of 1. If you draw the plot correctly by hand then there is no difference:
layout(matrix(1:2, ncol = 2))
plot(metaMDS(eurodist)$points, asp = 1, main = "aps = 1")
plot(metaMDS(eurodist), main = "plot.metaMDS")
layout(1)
There are good reasons why we provide S3 methods for things like scores and plot so you don't need to remember the details. If you go off piste, you do need to sweat the details.
This should now answer your main question; no metaMDS() does not do *principal coordinates analysis. If you want principal coordinates analysis, see ?capscale.
I have to plot a few different simple linear models on a chart, the main point being to comment on them. I have no data for the models. I can't get R to create a plot with appropriate axes, i.e. I can't get the range of the axes correct. I think I'd like my y-axis to 0-400 and x to be 0-50.
Models are:
$$
\widehat y=108+0.20x_1
$$$$
\widehat y=101+2.15x_1
$$$$
\widehat y=132+0.20x_1
$$$$
\widehat y=119+8.15x_1
$$
I know I could possibly do this much more easily in a different software or create a dataset from the model and estimate and plot the model from that but I'd love to know if there is a better way in R.
As #Glen_b noticed, type = "n" in plot produces a plot with nothing on it. As it demands data, you have to provide anything as x - it can be NA, or some data. If you provide actual data, the plot function will figure out the plot margins from the data, otherwise you have to choose the margins by hand using xlim and ylim arguments. Next, you use abline that has parameters a and b for intercept and slope (or h and v if you want just a horizontal or vertical line).
plot(x=NA, type="n", ylim=c(100, 250), xlim=c(0, 50),
xlab=expression(x[1]), ylab=expression(hat(y)))
abline(a=108, b=0.2, col="red")
abline(a=101, b=2.15, col="green")
abline(a=132, b=0.2, col="blue")
abline(a=119, b=8.15, col="orange")
So... I'm looking at an example in a book that goes something like this:
library(daewr)
mod1 <- aov(height ~ time, data=bread)
summary(mod1)
...
par(mfrow=c(2,2))
plot(mod1, which=5)
plot(mod1, which=1)
plot(mod1, which=2)
plot(residuals(mod1) ~ loaf, main="Residuals vs Exp. Units", font.main=1, data=bread)
abline(h = 0, lty = 2)
That all works... but the text is a little vague about the purpose of the parameter 'which='. I dug around in the help (in Rstudio) on plot() and par(), looked around online... found some references to a different 'which()'... but nothing really referring me to the purpose/syntax for the parameter 'which=' inside plot().
A bit later (next page, figures) I found a mention of using names(mod1) to view the list of quantities calculated by aov... which I presume is what which= is refering to, i.e. which item in the list to plot where in the 2x2 matrix of plots. Yay. Now where the heck is that buried in the docs?!?
which selects which plot to be displayed:
A plot of residuals against fitted values
A normal Q-Q plot
A Scale-Location plot of sqrt(| residuals |) against fitted values
A plot of Cook's distances versus row labels
A plot of residuals against leverages
A plot of Cook's distances against leverage/(1-leverage)
By default, the first three and 5 are provided.
Check ?plot.lm in r for more details.
I'm simply trying to display the fit I've generated using lm(), but the lines function is giving me a weird result in which there are multiple lines coming out of one point.
Here is my code:
library(ISLR)
data(Wage)
lm.mod<-lm(wage~poly(age, 4), data=Wage)
Wage$lm.fit<-predict(lm.mod, Wage)
plot(Wage$age, Wage$wage)
lines(Wage$age, Wage$lm.fit, col="blue")
I've tried resetting my plot with dev.off(), but I've had no luck. I'm using rStudio. FWIW, the line shows up perfectly fine if I make the regression linear only, but as soon as I make it quadratic or higher (using I(age^2) or poly()), I get a weird graph. Also, the points() function works fine with poly().
Thanks for the help.
Because you forgot to order the points by age first, the lines are going to random ages. This is happening for the linear regression too; he reason it works for lines is because traveling along any set of points along a line...stays on the line!
plot(Wage$age, Wage$wage)
lines(sort(Wage$age), Wage$lm.fit[order(Wage$age)], col = 'blue')
Consider increasing the line width for a better view:
lines(sort(Wage$age), Wage$lm.fit[order(Wage$age)], col = 'blue', lwd = 3)
Just to add another more general tip on plotting model predictions:
An often used strategy is to create a new data set (e.g. newdat) which contains a sequence of values for your predictor variables across a range of possible values. Then use this data to show your predicted values. In this data set, you have a good spread of predictor variable values, but this may not always be the case. With the new data set, you can ensure that your line represents evenly distributed values across the variable's range:
Example
newdat <- data.frame(age=seq(min(Wage$age), max(Wage$age),length=1000))
newdat$pred <- predict(lm.mod, newdata=newdat)
plot(Wage$age, Wage$wage, col=8, ylab="Wage", xlab="Age")
lines(newdat$age, newdat$pred, col="blue", lwd=2)
The command you suggested serves the purpose to some extent But I am still looking for a command/function to give smoother curves. I am giving a reproducible code below which is drawing rather sharp edges or more crudely: the line joining the points is too sharp especially at the moment it starts from one point to another. Sorry for non-technical language as I am still unfamiliar with the field. Thanks for the help provided on this issue. Please suggest the modifications in the code below to get smoother curves.
xdata1<-c(6:11)
ydata1<-c(-0.75132894, -1.71909555, -0.62653171, 0.49512191, 0.29201836, 0.31094460)
plot(NULL,NULL,xlim=xlims,ylim=ylims,axes=FALSE, ann=FALSE)
axis(1,cex.axis=0.7,mgp=c(3, .3, 0))
axis(2, las=1,cex.axis=0.7,at=c(-2,-1,0,1,2), mgp=c(3, .7, 0))
mtext(side = 1, text =expression('Year'), line = 1,font=15)
mtext(side = 2, text = expression('Variable'), line = 1.5,font=15)
lines(smooth.spline(xdata1,ydata1, df=5), col='red',type="l", pch=22, lty=1, lwd=1)
#######Updated Query Finishes Here
I have to plot very small magnitude data against time steps of 1. Please see the sample data below:
xdata<-c(1:48)
ydata<- c(0.325563413,0.401913414,0.221939845,0.19881055,
-0.05918293,-1.108143815,-0.220563332,-0.148715078,
-0.14998762,0.131610695,0.249923598,0.246891873,
0.656812019,0.524436114,0.23875397,0.200695075,
-0.015974087,-0.611863249,0.121994831,-0.143103421,
-0.142109609,0.101451935,0.160242421,0.232404601,
0.348305745,0.231109382,0.334988321,0.263046902,-0.058154333,
-1.032276818,-0.352068888,-0.13082767,-0.134611511,0.116967421,
0.268706409,0.232776855,0.39515544,0.540317537,0.424281195,
0.3061158,-0.210735495,0.023705618,0.473338271,0.270527033,
-0.165394174,0.268773501,0.202437269,0.305577906)
Please help me in plotting a smooth line without highlighting individual points for the data.
Thanks in advance,
Not sure what "without highlighting individual points" means, but here's one way to get a smooth line:
plot(xdata,ydata)
lines(smooth.spline(xdata,ydata, df=10), col = "red")
See also: ?loess.smooth, apropos("smooth"). Searching for "[r] smooth plot" finds How to fit a smooth curve to my data in R? ...
Specifying the type to "l" in plot will just give you lines.
plot(xdata, ydata, type = "l")