Problems with trendline in correlation graph

Hello everyone!
First of all, I apologize for my terrible English (it is not my native language).

Well, I really need the help of one of you, please!

I am having a very big problem with the generation of graphs for correlating two variables. In fact, when I need to add the trendline.

In the correlations script (below), there are four correlations using Spearman's coefficient.
Three are showing results where the rho is positive and the graph generated shows the correlations and the trendline as expected.

However, in one of the correlations (StsA x Succ, the first) the result of the Spearman coefficient is negative (rho = -0.4), so the trendline should be decreasing in the graph. Am I right?

It happens that when I add the line, it is introduced in the graph in a (slightly) increasing way.

Where am I going wrong? can anybody help me?

Hug! :slight_smile:

#StsA x Succ
StsA=c(0.75, 0.74, 0.84, 0.69, 0.71)
Succ=c(0.24, 0.25, 0.54, 0.55, 0.50)
cor.test(StsA,Succ,method="spearman")
plot(StsA,Succ)
lm(Succ ~ StsA) 
abline(lm(Succ ~ StsA)) #"Problem" is here! Rho=-0,4 line decreasing, right?

#InsA x Insu
InsA=c(0.07, 0.04, 0.06, 0.03, 0.05)
Insu=c(0.75, 0.74, 0.45, 0.44, 0.49)
cor.test(InsA,Insu,method="spearman")
plot(InsA,Insu)
lm(Insu ~ InsA)
abline(lm(Insu ~ InsA))

#StsB x Succ
StsB=c(0.67, 0.65, 0.78, 0.76, 0.72)
Succ=c(0.24, 0.25, 0.54, 0.55, 0.50)
cor.test(StsB,Succ,method="spearman")
plot(StsB,Succ)
lm(Succ ~ StsB)
abline(lm(Succ ~ StsB))

#InsB x Insu
InsB=c(0.10, 0.11, 0.03, 0.01, 0.05)
Insu=c(0.75, 0.74, 0.45, 0.44, 0.49)
cor.test(InsB,Insu,method="spearman")
plot(InsB,Insu)
lm(Insu ~ InsB)
abline(lm(Insu ~ InsB))

Hi, and welcome!

Thanks for using reprex!

English is a world language, and there's no good, average or terrible way to use it. The purpose of any language is to communicate clearly above all else. Only secondarily are the fine points of written English as it is used in some some settings important.

Ok, let's take a look at the structure of the objects to be plotted.

StsA=c(0.75, 0.74, 0.84, 0.69, 0.71)
Succ=c(0.24, 0.25, 0.54, 0.55, 0.50)
fit1 <- cor.test(StsA,Succ,method="spearman", alternative = "two.sided")
# plot(StsA,Succ)
fit2 <- lm(Succ ~ StsA) 
# abline(lm(Succ ~ StsA)) #"Problem" is here! Rho=-0,4 line decreasing, right?
str(fit1)
#> List of 8
#>  $ statistic  : Named num 28
#>   ..- attr(*, "names")= chr "S"
#>  $ parameter  : NULL
#>  $ p.value    : num 0.517
#>  $ estimate   : Named num -0.4
#>   ..- attr(*, "names")= chr "rho"
#>  $ null.value : Named num 0
#>   ..- attr(*, "names")= chr "rho"
#>  $ alternative: chr "two.sided"
#>  $ method     : chr "Spearman's rank correlation rho"
#>  $ data.name  : chr "StsA and Succ"
#>  - attr(*, "class")= chr "htest"
str(fit2)
#> List of 12
#>  $ coefficients : Named num [1:2] 0.336 0.107
#>   ..- attr(*, "names")= chr [1:2] "(Intercept)" "StsA"
#>  $ residuals    : Named num [1:5] -0.1764 -0.1654 0.114 0.14 0.0878
#>   ..- attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
#>  $ effects      : Named num [1:5] -0.9302 0.0123 0.0847 0.2462 0.176
#>   ..- attr(*, "names")= chr [1:5] "(Intercept)" "StsA" "" "" ...
#>  $ rank         : int 2
#>  $ fitted.values: Named num [1:5] 0.416 0.415 0.426 0.41 0.412
#>   ..- attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
#>  $ assign       : int [1:2] 0 1
#>  $ qr           :List of 5
#>   ..$ qr   : num [1:5, 1:2] -2.236 0.447 0.447 0.447 0.447 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : chr [1:5] "1" "2" "3" "4" ...
#>   .. .. ..$ : chr [1:2] "(Intercept)" "StsA"
#>   .. ..- attr(*, "assign")= int [1:2] 0 1
#>   ..$ qraux: num [1:2] 1.45 1.06
#>   ..$ pivot: int [1:2] 1 2
#>   ..$ tol  : num 1e-07
#>   ..$ rank : int 2
#>   ..- attr(*, "class")= chr "qr"
#>  $ df.residual  : int 3
#>  $ xlevels      : Named list()
#>  $ call         : language lm(formula = Succ ~ StsA)
#>  $ terms        :Classes 'terms', 'formula'  language Succ ~ StsA
#>   .. ..- attr(*, "variables")= language list(Succ, StsA)
#>   .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : chr [1:2] "Succ" "StsA"
#>   .. .. .. ..$ : chr "StsA"
#>   .. ..- attr(*, "term.labels")= chr "StsA"
#>   .. ..- attr(*, "order")= int 1
#>   .. ..- attr(*, "intercept")= int 1
#>   .. ..- attr(*, "response")= int 1
#>   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   .. ..- attr(*, "predvars")= language list(Succ, StsA)
#>   .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#>   .. .. ..- attr(*, "names")= chr [1:2] "Succ" "StsA"
#>  $ model        :'data.frame':   5 obs. of  2 variables:
#>   ..$ Succ: num [1:5] 0.24 0.25 0.54 0.55 0.5
#>   ..$ StsA: num [1:5] 0.75 0.74 0.84 0.69 0.71
#>   ..- attr(*, "terms")=Classes 'terms', 'formula'  language Succ ~ StsA
#>   .. .. ..- attr(*, "variables")= language list(Succ, StsA)
#>   .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#>   .. .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. .. ..$ : chr [1:2] "Succ" "StsA"
#>   .. .. .. .. ..$ : chr "StsA"
#>   .. .. ..- attr(*, "term.labels")= chr "StsA"
#>   .. .. ..- attr(*, "order")= int 1
#>   .. .. ..- attr(*, "intercept")= int 1
#>   .. .. ..- attr(*, "response")= int 1
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   .. .. ..- attr(*, "predvars")= language list(Succ, StsA)
#>   .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#>   .. .. .. ..- attr(*, "names")= chr [1:2] "Succ" "StsA"
#>  - attr(*, "class")= chr "lm"

Created on 2020-03-05 by the reprex package (v0.3.0)

Where is rho?

1 Like

Oh, thank you for your kindness and attention, Richard!
The important thing is to be able to communicate, isn't it? :slight_smile:

But, continuing ...

When running cor.test, the result of rho was - 0.4.
Whith this result (rho negative), when I introduce a trend line in the plotted graph, the slope of the line should be negative (from the top to bottom), right?

1 Like

¡De nada!

Let's take a closer look

suppressPackageStartupMessages(library(ggplot2)) 
StsA=c(0.75, 0.74, 0.84, 0.69, 0.71)
Succ=c(0.24, 0.25, 0.54, 0.55, 0.50)
obj <- as.data.frame(cbind(StsA,Succ))
p <- ggplot(obj, aes(StsA,Succ))
p + geom_point() + geom_smooth(method = "lm")

Created on 2020-03-05 by the reprex package (v0.3.0)

The hardest thing that I had to go through as a beginning was to carefully look at the object that I was giving to a function. Finally, I realize that almost all of R that the user sees is nothing more than school algebra, f(x) = y. That means, of course, whenever y is unexpected, the users should look first at x and only then at f. And when one does look at f, the problem is very often that f wants an argument different from what it was given.

Okay, Richard!
I will use these observations in the research!
Thank you very much (once more) for your patience and help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.