Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ object Stats {

val R2_ADJ = DataFrame.Variable("..adjr2..", STAT, "adjr2")
val R2 = DataFrame.Variable("..r2..", STAT, "r2")
val METHOD = DataFrame.Variable("..method..", STAT, "method")
val AIC = DataFrame.Variable("..aic..", STAT, "aic")
val BIC = DataFrame.Variable("..bic..", STAT, "bic")
val F = DataFrame.Variable("..f..", STAT, "f")
val DF1 = DataFrame.Variable("..df1..", STAT, "df1")
val DF2 = DataFrame.Variable("..df2..", STAT, "df2")
val P = DataFrame.Variable("..p..", STAT, "p")
val CI_LEVEL = DataFrame.Variable("..cilevel..", STAT, "cilevel")
val CI_LOW = DataFrame.Variable("..cilow..", STAT, "cilow")
val CI_HIGH = DataFrame.Variable("..cihigh..", STAT, "cihigh")

val SCALED = DataFrame.Variable("..scaled..", STAT, "scaled")

Expand Down Expand Up @@ -79,6 +89,16 @@ object Stats {
GROUP,
R2,
R2_ADJ,
METHOD,
AIC,
BIC,
F,
DF1,
DF2,
P,
CI_LEVEL,
CI_LOW,
CI_HIGH,
)

val result = HashMap<String, DataFrame.Variable>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class LinearRegression private constructor (
meanX: Double,
sumXX: Double,
model: (Double) -> Double,
xVals: DoubleArray,
yVals: DoubleArray,
standardErrorOfEstimate: Double,
tCritical: Double,
eq: List<Double>,
r2: Double,
) : RegressionEvaluator(n, meanX, sumXX, model, standardErrorOfEstimate, tCritical, eq, r2) {
) : RegressionEvaluator(n, meanX, sumXX, model, xVals, yVals, standardErrorOfEstimate, tCritical, eq) {
companion object {
fun fit(xs: List<Double?>, ys: List<Double?>, confidenceLevel: Double): LinearRegression? {
check(xs, ys, confidenceLevel)
Expand Down Expand Up @@ -45,10 +46,11 @@ class LinearRegression private constructor (
meanX,
sumXX,
model,
xVals,
yVals,
calcStandardErrorOfEstimate(xVals, yVals, model, degreesOfFreedom),
calcTCritical(degreesOfFreedom, confidenceLevel),
listOf(intercept, slope),
calcRSquared(xVals, yVals, model)
listOf(intercept, slope)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class LocalPolynomialRegression private constructor (
meanX: Double,
sumXX: Double,
model: (Double) -> Double,
xVals: DoubleArray,
yVals: DoubleArray,
standardErrorOfEstimate: Double,
tCritical: Double,
r2: Double,
) : RegressionEvaluator(n, meanX, sumXX, model, standardErrorOfEstimate, tCritical, emptyList(), r2) {
tCritical: Double
) : RegressionEvaluator(n, meanX, sumXX, model, xVals, yVals, standardErrorOfEstimate, tCritical, emptyList()) {
companion object {
fun fit(xs: List<Double?>, ys: List<Double?>, confidenceLevel: Double, bandwidth: Double): LocalPolynomialRegression? {
check(xs, ys, confidenceLevel)
Expand Down Expand Up @@ -44,9 +45,10 @@ class LocalPolynomialRegression private constructor (
meanX,
sumXX,
model,
xVals,
yVals,
calcStandardErrorOfEstimate(xVals, yVals, model, degreesOfFreedom),
calcTCritical(degreesOfFreedom, confidenceLevel),
calcRSquared(xVals, yVals, model),
calcTCritical(degreesOfFreedom, confidenceLevel)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class PolynomialRegression private constructor (
meanX: Double,
sumXX: Double,
model: (Double) -> Double,
xVals: DoubleArray,
yVals: DoubleArray,
standardErrorOfEstimate: Double,
tCritical: Double,
eq: List<Double>,
r2: Double,
) : RegressionEvaluator(n, meanX, sumXX, model, standardErrorOfEstimate, tCritical, eq, r2) {
eq: List<Double>
) : RegressionEvaluator(n, meanX, sumXX, model, xVals, yVals, standardErrorOfEstimate, tCritical, eq) {
companion object {
fun fit(xs: List<Double?>, ys: List<Double?>, confidenceLevel: Double, deg: Int): PolynomialRegression? {
check(xs, ys, confidenceLevel)
Expand Down Expand Up @@ -47,10 +48,11 @@ class PolynomialRegression private constructor (
meanX,
sumXX,
model,
xVals,
yVals,
calcStandardErrorOfEstimate(xVals, yVals, model, degreesOfFreedom),
calcTCritical(degreesOfFreedom, confidenceLevel),
polynomial.getCoefficients(),
calcRSquared(xVals, yVals, model)
polynomial.getCoefficients()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,16 @@ import kotlin.math.pow
import kotlin.math.sqrt

abstract class RegressionEvaluator protected constructor(
private val n: Int,
val n: Int,
private val meanX: Double,
private val sumXX: Double,
private val model: (Double) -> Double,
val model: (Double) -> Double,
val xVals: DoubleArray,
val yVals: DoubleArray,
private val standardErrorOfEstimate: Double,
private val tCritical: Double,
val eq: List<Double>,
val r2: Double,
val eq: List<Double>
) {
val adjR2: Double
get() {
val predictorsCount = (eq.size - 1).coerceAtLeast(0)
if (n <= predictorsCount + 1 || r2.isNaN()) {
return Double.NaN
}
return 1.0 - (1.0 - r2) * ((n - 1.0) / (n - predictorsCount - 1.0))
}

fun value(x: Double): Double {
return model(x)
Expand Down Expand Up @@ -84,33 +77,5 @@ abstract class RegressionEvaluator protected constructor(
Double.NaN
}
}

fun calcRSquared(
xVals: DoubleArray,
yVals: DoubleArray,
model: (Double) -> Double
): Double {
val meanY = yVals.average()

var ssTot = 0.0
var ssRes = 0.0

for (i in xVals.indices) {
val y = yVals[i]
val yHat = model(xVals[i])

val diffRes = y - yHat
ssRes += diffRes * diffRes

val diffMean = y - meanY
ssTot += diffMean * diffMean
}

return if (ssTot == 0.0) {
0.0
} else {
1.0 - ssRes / ssTot
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package org.jetbrains.letsPlot.core.plot.base.stat.regression

import org.jetbrains.letsPlot.core.plot.base.stat.math3.Percentile
import org.jetbrains.letsPlot.core.commons.data.SeriesUtil
import org.jetbrains.letsPlot.core.plot.base.stat.math3.Percentile
import kotlin.random.Random

internal object RegressionUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.jetbrains.letsPlot.core.plot.base.FormatterUtil
import org.jetbrains.letsPlot.core.plot.base.PlotContext
import org.jetbrains.letsPlot.core.plot.base.data.DataFrameUtil

open class DataFrameField(
class DataFrameField(
private val name: String,
private val format: String? = null
) : ValueSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ open class PlotConfigBackend(

companion object {

private val SMOOTH_STAT_VARS_TO_KEEP = listOf(
Stats.R2,
Stats.R2_ADJ,
Stats.N,
Stats.AIC,
Stats.BIC,
Stats.METHOD,
Stats.F,
Stats.DF1,
Stats.DF2,
Stats.P,
Stats.CI_LEVEL,
Stats.CI_LOW,
Stats.CI_HIGH
)

private fun variablesToKeep(facets: PlotFacets, layerConfig: LayerConfig): Set<String> {
val stat = layerConfig.stat
// keep all original vars
Expand Down Expand Up @@ -334,7 +350,7 @@ open class PlotConfigBackend(
varsToKeep.removeAll(notRenderedVars)
varsToKeep.addAll(renderedVars)

varsToKeep.addAll(listOf(Stats.R2, Stats.R2_ADJ))
varsToKeep.addAll(SMOOTH_STAT_VARS_TO_KEEP)
varsToKeep.addAll(layerConfig.ownData.variables().filter { it.label.contains("smooth_eq_coef_") })

return HashSet<String>() +
Expand Down
2 changes: 0 additions & 2 deletions python-package/lets_plot/plot/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ def __init__(self, variables: List[str] = None):
self._label_x = None
self._label_y = None

self.inherit_color()

def eq(self, lhs=None, rhs=None, format=None, threshold=None) -> "smooth_labels":

"""
Expand Down