R/profile.uiols.R

Defines functions profile.uiols

Documented in profile.uiols

#' Plot of UI and CI
#'
#' Plot function for objects returned from \code{\link{ui_ols}}.
#' Plots confidence intervals for different values of rho and the uncertainty interval.
#' @param x An object of class uiols
#' @param plot.all If TRUE, plots all covariates.
#' @param which Specify which variables should be plotted by either sending in their names in a vector or a vector with their numbers (1 intercept, 2 for the first covariate etc.).
#' @param intercept If TRUE, also plots the intercept.
#' @param xlab Title for x-axis, default is \code{expression(rho)}.
#' @param ylab Title for y-axis, default is the variable names.
#' @param scales Fixed or different scales of the y axis between panels. Default is "fixed", the other choice is "free_y".
#' @param expand Add space by expanding y-axis range. Default is 0.1 which is 10% extra on above and below.
#' @param ... Additional arguments, use is discouraged.
#'
#'@importFrom ggplot2 ggplot aes geom_ribbon geom_line geom_hline facet_wrap scale_y_continuous labs theme_minimal expansion
#' @export


profile.uiols<-function(x, plot.all=TRUE, which=NA, intercept=FALSE, xlab=NULL,ylab=NULL, scales="fixed", expand=0.1, ...){
p<-dim(x$coef)[1]-1
nrho<-dim(x$coef)[2]
if(is.null(ylab)){NamnX<-rownames(x$coef)
	}else{if(length(as.vector(ylab))==1){
	NamnX<-rep(ylab,length(rownames(x$coef)))
	}else{
		if(length(as.vector(ylab))==length(rownames(x$coef))){
	NamnX==ylab	}else{
		stop('Wrong dimensions on ylab.')
	}}}
if(is.null(xlab)){xlab=expression(rho)}

if(sum(is.na(which)>0)){
	if(plot.all==FALSE) warning('Need to specify which variable in order to not plot all.')
	plot.all=TRUE
}else{
	plot.all=FALSE
	if(mode(which)=='character'){
		which<-which(NamnX %in% which)
	}
}
if(plot.all){
	if(intercept){which<- 1:(p+1)}else{which<- 2:(p+1)}
	}


plot_data <- data.frame()
for (i in which) {
  df <- data.frame(
    rho = x$gridrho,
    coef = x$coef[i, ],
    ci_low = x$ci[i, , 1],
    ci_high = x$ci[i, , 2],
    variable = NamnX[i]
  )
  plot_data <- rbind(plot_data, df)
}

ggplot(plot_data, aes(x = rho, y = coef)) +
  geom_ribbon(aes(ymin = ci_low, ymax = ci_high), fill = "grey",alpha=0.5) +
  geom_line(size=1) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  facet_wrap(~variable, scales=scales) +
  scale_y_continuous(expand = expansion(mult = c(expand, expand))) +
  labs(x = xlab, y = NULL,title=paste0("Profile of estimates and ",100*(1-2*pnorm(-x$zalpha)) ,"% CI for different values of rho")) +
  theme_minimal(base_size = 14)

}

Try the ui package in your browser

Any scripts or data that you put into this service are public.

ui documentation built on June 25, 2026, 5:09 p.m.