0

model plotOk, I am having real trouble in R getting my interaction terms to plot accurately. Firstly here is my glm.

Temp.1<-glm(black ~ abs(Lat) + Alt.1 + Max.1 + abs(Lat):Max.1 + abs(Lat):Alt.1)

My altitude and maximum temperature variables are centred, and I get the following output:

Coefficients:
(Intercept)  abs(Lat)    Alt.1      Max.1      abs(Lat):Max.1  abs(Lat):Alt.1  
51.7399258   -0.4404355  0.0002218 -7.7018000  0.8700034      -0.0015007  

I need to plot the results of my model - one showing the effect of proportion blackness with temperature when latitude changes, and one with the difference in proportion blackness with altitude at different latitudes.

The problem I am having is R doesn't seem to plot the models correctly, and is giving me values for blackness above and below my scale of 0-100 (not possible as its a percentage)

This is the code I am using and am completely stuck!

TempA<-seq(-2,5)
TempB<-(TempA+30.35396)
AltA<-seq(60,2000)
LatA<-seq(0,27)

BML<-51.7399258+(-7.7018000*TempA) ## equator
BMLLow<-(51.7399258+(0.6*-0.4404355))+(-7.7018000+(0.8700034*0.6)*TempA)
BMLHigh<-51.7399258+(24*-0.4404355)+(-7.7018000+(0.8700034*24)*TempA)

plot(Max,black, ylim=c(0,100), xlim=c(26,38) 
lines(TempB,BML)
lines(TempB,BMLLow, col="red")
lines(TempB,BMLHigh, col="blue")

I would really appreciate all or any help as I have no idea how to fix this!

Thanks in advance

Here is some sample data, I'm afraid I can't share any more but this is the format i have, but I am running the code with the temperature and altitude centred.

dput(Stack[1:10,]) structure(list(Latitude = c(-24.30795, -23.340241, -21.15665, -21.15665, -17.87762, -17.864179, -17.820673, -17.798579, -17.767033, -17.755872), Max_Mean = c(31.0208333, 31.0208333, 31.0208333, 31.0208333, 30.7736083, 30.7736083, 30.7736083, 30.7736083, 30.7736083, 30.7736083), Mean_black_pack = c(50.422529, 31.11580776, 34.6917816, 33.86864311, 47.64279486, 3.4046481, 34.26923913, 27.41466269, 18.12089591, 42.71618832), Mean_Alt = c(1197.508352, 1016.849649, 951.0622235, 951.0622235, 978.1990858, 1010.503054, 980.9060909, 982.4801458, 984.3482196, 984.0309493)), .Names = c("Latitude", "Max_Mean", "Mean_black_pack", "Mean_Alt"), row.names = c(NA, 10L), class = "data.frame")

Thank you for all the help These are the variables as I have them, then for altitude and latitude Alt.1 = centred altitude and Max.1= centred Max_Mean

black<-Stack$Mean_black_pack
Max<-Stack$Max_Mean
Lat<-Stack$Latitude
Alt<-Stack$Mean_Altitude
Alt.1<-Alt-(mean(Alt))
Max.1<-Max-(mean(Max))
  • Please provide a [reproducible example](http://stackoverflow.com/a/5965451/496488) so that we can more easily help you. – eipi10 Apr 19 '16 at 17:08
  • Even though your outcome is between 0% and 100%, linear regression (the default for `glm`) can give you predictions outside this range. Perhaps beta regression would be more appropriate here. [This tutorial on the R `betareg` package](https://cran.r-project.org/web/packages/betareg/vignettes/betareg.pdf) might be helpful. – eipi10 Apr 19 '16 at 17:15
  • I added some data, i hope it helps, sorry if its in the wrong format i'm new to stack. –  Apr 19 '16 at 19:36
  • A reproducible example would allow us to reproduce your model and show you a better way to plot the predictions. Also, paste in the output of `dput(my_data_sample)` rather than the difficult-to-use data in the current version of your question. – eipi10 Apr 19 '16 at 19:40
  • Unfortunately I can't give a larger sample as its a huge data set and I'm restricted by data-sharing agreements. I'm also confused by the `dput(my_data_sample)` do I run this in r? –  Apr 19 '16 at 19:53
  • We neither need nor want a large data set. We need a small sample that contains all the columns needed to create your model and exhibit the problems you want to solve. It can even be fake data, so long as it exhibits the same issues as your real data and model. – eipi10 Apr 19 '16 at 19:59
  • ok but how should i give it to you, if the way i currently have it isn't right. Sorry for seeming so dim i am not very good at stats or computers –  Apr 19 '16 at 20:19
  • As noted in an earlier comment, use `dput()`. For example, to provide the first ten rows of your data frame, paste in the output of `dput(my_data[1:10,])`. I'm not saying you should provide the first ten rows. Just select a small sample that can be run with your model code to reproduce the issue. – eipi10 Apr 19 '16 at 20:26
  • It also appears that my code is not plotting the intercept where you would expect (at least when i calculate it by hand) –  Apr 19 '16 at 21:02
  • The variables in your data sample are `Latitude Max_Mean Mean_black_pack Mean_Alt`. These are not the same as the variables in your model, which are `black, Lat, Alt.1, Max.1`. I could assume a correspondence (e.g., Max_Mean = Max.1, etc.) but it would really be a lot easier if you made your question *reproducible*. – eipi10 Apr 19 '16 at 21:06

0 Answers0