How to fit b-spline regression in R? 
I want to create a function to estimate a time varying model with B splines such that:
  $$ Y_i = \sum_{j=0}^p (B(T_i)X_{ij})^T \gamma_{0j} + \hat{\epsilon}_i$$
  on an equally spaced B-spline basis $B(x) = (B_1(x),...,B_L(x))^T$ on $[0,1]$ where $L=c_Ln^{1/5}$ and the degree of the basis is at least 2.

I didn't understand the meaning of $L$ here, how is this implemented, it seems $\gamma_{0j} \in \mathbb{R}^L$ and $B(T_i)X_{ij} \in \mathbb R{}^L$.  Could someone verify this code?
#Simulated Data.. (time varying model)
  n = 200
  X <- matrix(rnorm(1000*n),nrow = n)
  U <- matrix(runif(2*n),nrow = n)
  T <- tcrossprod(U,t(c(0,1)))
  epsilon <- rnorm(n,0,1)
  Y <- 2*X[,1] + 3*T*X[,2] + X[,3]*(T+1)^2+ 4*X[,4]*sin(2*pi*T)/(2-sin(2*pi*T))+1*epsilon

#library(splines2)
L = n^(1/5) 
bsMat <<- bSpline(X, degree = 2, intercept = TRUE)


This isn't right because the spline matrix is of dimension 200000x3... where X is a 200x 1000 matrix (so clearly it has multiplied the rows and columns of X to get bsMat dimension). This therefore gives gives non-conformable arguments when i apply a time varying regression:
library(tvReg)
tvm =  tvLM(Y ~ bsMat) 

 A: You can estimate your model using lm() function. The fact is that your model matrix is not correctly defined. To make it clearer I will use a simpler example. You will then be able to extend the reasoning to your additive structure (you might want to give a look to the mgcv package). I will consider the model:
$$
y = x(t) f(t) + \epsilon
$$
with $\epsilon \sim N(0, \sigma_{\epsilon})$ and $x(t)$ is the (observed/predictor) variable with which you would like to "interact" your smooth effect $f(t)$. The model can be re-written as
$$
y = \mbox{diag}(x) B \alpha + \epsilon
$$
where $B \alpha$ is the B-spline approximation of $f(t)$. To make a concrete example, we can simulate some data as follows:
$$
y = 0.2 + 2 \sin(t) * x(t) + \epsilon
$$
where, $x(t)$ will be $\cos(t/2)$.
Below you will find a small R-code. I will use the splineDesign() function in the splines package to compute the B-spline matrix on equally spaced internal knots. I left some comments in the code. I hope it is clear enough and helps for the original question.
set.seed(2015)
library(splines)

# Simulated Data.. (time varying model)
N      = 200
time   = seq(0, 2 * pi, len = N)
sd_sim = 0.1
xt     = cos(time/2)
eps    = rnorm(N, mean = 0, sd = sd_sim)
ft     = 2 * sin(time) 
y      = 0.2 + ft * xt + eps 

# Create B-splines
deg   = 3
ndx   = deg * N^(1/5) 
xr    = max(time)
xl    = min(time)
xmax  = xr + 0.01 * (xr - xl)
xmin  = xl - 0.01 * (xr - xl)
dt    = (xmax - xmin) / ndx
knots = seq(xmin - deg * dt, xmax + deg * dt, by = dt)
B     = splineDesign(knots = knots, x = time, ord = deg + 1, derivs = 0,outer.ok = TRUE)

# Create model matrix
X  = diag(xt)
Mm = X %*% B

# Fit model
fit = lsfit(Mm, y, intercept = T)

# Plot results - yhat, f(t)
par(mfrow = c(2, 1), mar = c(3,3,3,3))
plot(time, y, main = 'Data and fit')
lines(time, cbind(1, Mm) %*% fit$coefficients, col = 2)

plot(time, ft, main = 'Time-varying component', ylim = c(2.5, -2.5))
lines(time, B %*% fit$coefficients[-1], col = 2)


A: I am not quite sure how you want to construct your spline basis matrix, but normally you can use 1 variable, and construct a matrix for its $x,x^2,x^3$ terms.. And you add this for each term.
In your code:
bSpline(X, degree = 2, intercept = TRUE)

You get 200000x3 because X is treated 1 variable, the matrix is used as a vector and you get a matrix the length of your matrix X.. If you want a spline basis for each of your columns in X, you can do:
bsMat = lapply(1:ncol(X),function(i)bSpline(X[,i],degree=2,intercept=TRUE))
bsMat = do.call(cbind,bsMat)

You have now 3000 columns, and you can't fit this with a linear model and I am not so sure if this is actually what you intend. You can check this summary about how splines are used, and maybe redefine you questions. 
If the spline is only intended for a few columns, then use the code above to do lapply( ..), join it with your other predictors and regress.
